Vba 在上找到了哪一行.Find
我有下面的代码,这在技术上是一个嵌套循环Vba 在上找到了哪一行.Find,vba,excel,Vba,Excel,我有下面的代码,这在技术上是一个嵌套循环 Dim compareRange As Range Dim toCompare As Range Dim rFound As Range Dim cel As Range Set compareRange = Worksheets("sheet2").Range("A2:A" & Lastrow3) Set toCompare = Worksheets("sheet3").Range("A2:A" & Lastrow4) Set rFo
Dim compareRange As Range
Dim toCompare As Range
Dim rFound As Range
Dim cel As Range
Set compareRange = Worksheets("sheet2").Range("A2:A" & Lastrow3)
Set toCompare = Worksheets("sheet3").Range("A2:A" & Lastrow4)
Set rFound = Nothing
For Each cel In toCompare
Set rFound = compareRange.Find(cel)
如何在单独的工作表中找到该值所在的行?例如,如果AAAA位于sheet3的第1行,并且在sheet2的第5行找到它,那么如何检索第5行的值5?FIND返回对单元格的引用。从该引用可以访问单元格的所有属性,与手动设置单元格引用的方式相同
使用FINDNEXT或FINDPREVIOUS移动到找到的项的下一个或上一个实例
下面的代码显示了如何从每个找到的项返回各种值:
Sub Test()
Dim compareRange As Range
Dim toCompare As Range
Dim rFound As Range
Dim cel As Range
Dim FirstAddress As String
Dim LastRow3 As Long
Dim LastRow4 As Long
LastRow3 = 189: LastRow4 = 9
Set compareRange = Worksheets("sheet2").Range("A2:A" & LastRow3)
Set toCompare = Worksheets("sheet3").Range("A2:A" & LastRow4)
With compareRange
For Each cel In toCompare
'Find the first instance of cel.
Set rFound = .Find(cel)
'Check that rFound contains a value otherwise an error will occur when
'trying to return values from it.
If Not rFound Is Nothing Then
FirstAddress = rFound.Address
Do
With rFound
Debug.Print "Row: " & .Row & " - Col: " & .Column & _
" - Sheet: " & .Parent.Name & " - Book: " & .Parent.Parent.Name
End With
'Find the next instance of cel.
Set rFound = .FindNext(rFound)
Loop While FirstAddress <> rFound.Address
End If
Next cel
End With
End Sub
只需使用rFound.Row,否?^^^但在If Not rFound中为Nothing Then If block,除非您的数据的结构确保Find始终能找到匹配项。如果我的工作表一有多个值,例如第1行a列有111个值,第2行a列有111个值,则Find将始终能找到相同的值。对吗?程序将遍历范围从左到右,从上到下,因此它将始终查找最左边和最上面的单元格。您可以使用findnextrfund查找第2行上的下一个匹配值111。在“查找并保存返回的地址”对话框中,将FINDNEXT添加到循环中,并继续检查地址,直到再次返回到第一个地址。