2013-10-09 31 views
0

我需要從一個片材與另一個片材的另一行比較的基行比較行。底片將始終使用範圍A8,B8,C8和D8。工作表2行將隨着行的添加或刪除而動態變化,但總是使用列A,B,C和D.例如,這次可能有3行,並且包含5行用於下一次比較。但是,比較將始終從表2中的第3行開始,直到進行匹配或用盡行。如果基紙的A8與紙張2的A3匹配,則用紙張2的B3檢查基紙的B8。如果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 

如何使用?將代碼粘貼到模塊中。
您現在可以使用UDF「FIND_PLUS」。
在任何單元格中使用公式。
第一個參數是你要搜索的範圍(在你的情況下,它的工作表Sheet1 A8:D8)
第二個參數是你想搜索匹配的範圍內。 (Sheet2中A3:!?d)
在您的公式中輸入的單元格將返回TRUE如果有一個匹配
FALSE否則。

如果萬一你不需要的功能,這將讓你至少開始。
我已經對特定行發表了評論,以指導您的代碼的功能。

+0

協調理念對我來說意味着什麼。如果我想到,我不會首先要求幫助。謝謝。我沒有足夠的信譽來給你的答案+1,但是我認爲如果我只是需要查找是否有比賽而不必返回一個行號就可以工作。 –