我需要将一张纸的基行与另一张纸的另一行进行比较。基础表将始终使用范围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。我喜欢你的concantenating的想法所以我在想这样的事情。如果我离开基地并且有更简单的方法,请告诉我。我的骄傲并不容易瘀伤。
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
答案 0 :(得分: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
。
如果您不需要功能,这至少可以让您入门 我已经对特定行添加了注释,以指导您编写代码的功能。
答案 1 :(得分: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