2013-10-09 4 views
0

한 시트의 기본 행과 다른 시트의 다른 행을 비교해야합니다. 기준 시트는 항상 범위 A8, B8, C8 및 D8을 사용합니다. 시트 2 행은 행이 추가되거나 삭제됨에 따라 동적으로 변경되지만 항상 A, B, C 및 D 열을 사용합니다. 예를 들어 이번에는 3 행을 포함 할 수 있으며 다음 비교를 위해 5 행을 포함 할 수 있습니다. 그러나 비교는 항상 시트 2의 행 3에서 시작하여 일치가 이루어 지거나 행이 부족할 때까지 진행됩니다. 기본 시트의 A8이 용지 2의 A3과 일치하면 기본 시트의 B8을 용지 2의 B3으로 확인합니다. A8이 A3과 일치하지 않으면 다음 행으로 이동하여 A4로 A8을 확인하는 식으로 진행합니다. 기본 행의 열 A가 시트 2의 열 A와 일치하는지 (B는 B와 일치하고 C는 C와 일치하고 D는 D와 일치하는지) 확인합니다. 기준 시트의 범위가 다른 시트의 범위와 일치하지 않으면 match = true가 될 때까지 비교할 다음 행을 확인하고 true를 반환하거나 그렇지 않으면 false를 반환합니다. 기준 시트의 열 A는 용지 2의 열 B, C 또는 D와 절대 일치하지 않습니다. 기준 시트의 B는 용지 2의 A, C 또는 D와 절대 일치하지 않습니다.별도의 워크 시트에서 행을 비교하십시오.

미리 도움을 주셔서 감사합니다. 제게 더 많은 정보가 필요하면 알려주세요.

정확합니다. 일치하는 행 번호를 반환하거나 일치하는 항목이 없으면 -1을 반환하는 함수를 찾고 있습니다. 나는 당신의 생각을 좋아해서 이렇게 생각하고 있습니다. 내가 근심에서 멀어지고 쉬운 방법이 있다면, 알려주세요. 나의 자존심은 쉽게 상처를주지 않습니다.

Public Function RangesMatchRow(RefSheet As Worksheet) As Integer 
''I need to be able to return matching row number 
Dim Rng, rng2, val As Range 
Dim baseStr, refStr As String 
Dim lastRow, i As Integer 
Dim BaseSheet As Worksheet 

Set BaseSheet = Sheets("Base") 
'Get the range you want to compare 
Set Rng = BaseSheet.Range("A8:D8") 
'And concantenate it 
For Each val In Rng.Cells 
    baseStr = baseStr & val.Value 
Next val 

lastRow = RefSheet.Range("A").Offset(sheetName.Rows.Count - 1, 0).End(xlUp).Row 
For i = 3 To lastRow ''It will always start with row three and go until the last row for column A 
    rng2 = sheetName.Range("Ai:Di") ''Not sure if this is right but i represents the row number 
    For Each val In rng2 
     refStr = refStr & val.Value 
    Next val 
    If StrComp(UCase(baseStr), UCase(refStr), vbTextCompare) = 0 Then ''If they match Then 
     RangesMatchRow = i ''Set RangesMatchRow equal to i 
     Return ''And return 
    End If 
Next 
    RangesMatchRow = -1 ''If no matches are found then return -1 

End Function 
+2

지금까지 시도한 것을 보여주십시오. – RBarryYoung

+0

코드가 없지만 밀도가 높은 질문은 결코 매우 흥미롭지 않습니다 ;-) –

+0

이것은 내가 알아 낸 것입니다. 아무도 더 나은 대답을 찾지 못하면 같은 문제를 해결하기 위해 다른 사람들의 답변 섹션에 추가 할 것입니다. –

답변

0
Public Function RangesMatchRow(textInColA As String) As Integer 
Dim rng, rng2, val As Range 
Dim baseStr, refStr As String 
Dim lastRow, i As Integer 
Dim sheetName, baseSheet As Worksheet 

Set sheetName = Sheets(textInColA) 
Set baseSheet = Sheets("Base") 
'Get the base range you want to compare 
Set rng = baseSheet.Range("A8:D8") 
'And concantenate it 
For Each val In rng.Cells 
    baseStr = baseStr & val.Value 
Next val 

lastRow = sheetName.Range("A1").Offset(sheetName.Rows.Count - 1, 0).End(xlUp).Row 
''Gives me the last row anything was entered in column A 
For i = 3 To lastRow 
    Set rng2 = sheetName.Range("A" & i & ":D" & i) 
    ''Concantenate reference row each time through the loop 
    For Each val In rng2.Cells 
     refStr = refStr & val.Value 
    Next val 
    ''Convert everything to uppercase to make it case insensitive 
    ''Compare the rows and return the row number if there is a match 
    If StrComp(UCase(baseStr), UCase(refStr), vbTextCompare) = 0 Then 
     RangesMatchRow = i 
     Exit Function 
    End If 
Next i 
    ''Return -1 if no matches are found 
    RangesMatchRow = -1 

End Function 
0

기능이 필요하다고 생각하십니까? 코드는 다음과 같습니다.

Function FIND_PLUS(lookup_value As Range, reference As Range) As Boolean 

Dim rng, val, rng2 As Range 
Dim str_2_match, formula As String 
Dim row_count, i As Double 
Dim search_fld() As Variant 

Set rng = lookup_value 
'first get the string to look for. 
'here we concatenated all the values instead of comparing cell by cell 
For Each val In rng.Cells 
    str_2_match = str_2_match & val.Value 
Next val 

row_count = reference.Rows.Count 
ReDim search_fld(1 To row_count) 'resize the array 

'here we made an array of the concatenated values of the range you want to search 
'we used simple resize and offset to go through all the rows of your selected range 
For i = 1 To row_count 
    Set rng2 = reference.Resize(1).Offset(i - 1) 
    For Each val In rng2.Cells 
     search_fld(i) = search_fld(i) & val.Value 
    Next val 
Next i 
'here is where we performn the actual look up 
With Application 

Select Case IsError(.Match(str_2_match, search_fld, 0)) 
Case False 
    FIND_PLUS = True 
Case Else 
    FIND_PLUS = False 
End Select 

End With 

End Function 

사용 방법? 코드를 모듈에 붙여 넣습니다.
이제 'FIND_PLUS'라는 UDF를 사용할 수 있습니다.
모든 셀에서 수식을 사용하십시오.
첫 번째 인수는 검색하려는 범위입니다 (해당 경우 Sheet1! A8 : D8)
두 번째 인수는 일치하는 항목을 검색하려는 범위입니다. (시트 2 A3 :!? D)
FALSE 그렇지 않으면 일치가 있다면 TRUE를 반환합니다 수식에 입력 한 셀에
.

기능이 필요하지 않은 경우 최소한 시작해야합니다.
코드가하는 것을 설명하기 위해 특정 행에 주석을 달았습니다.

+0

Concentenation 아이디어는 나를 위해 무엇을했는지입니다. 만약 내가 그걸 생각했다면 처음부터 도움을 요청할 필요가 없었을 것입니다. 감사. 나는 당신의 대답을 +1하기에 충분한 평판이 없다.하지만 행 번호를 반환하지 않고도 일치하는 것이 있는지 알아 내야한다면 그것은 효과가있을 것이라고 생각한다. –

관련 문제