我需要從一個片材與另一個片材的另一行比較的基行比較行。底片將始終使用範圍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
向我們展示您到目前爲止嘗試過的方法。 – RBarryYoung
問題都沒有代碼,但密集的要求都不是很有趣的;-) –
這是我想通了。如果沒有人提出更好的答案,我會將其添加到解答相同問題的其他人的答案部分。 –