我目前有一个看起来像这样的排序列:
Cat
Cat
Cat
Cat
Dog
Dog
Dog
Dog
我想找到cat的最后一个索引(在这种情况下是4)和dog的最后一个索引(在这种情况下是8)
我尝试了以下内容:
Dim Last_Index_F As Long
With Sheets("Sheet1")
Last_Index_F = Range("A:A").Find(what:="Cat", after:=Range("A2"), searchdirection:=x1Previous).row
End With
MsgBox (Last_Index_F)
但是我得到了第一个跟随A2的猫的指数。有什么想法吗?
答案 0 :(得分:2)
代码就是这样。
Sub test()
Dim Last_Index_F As Long
Dim Ws As Worksheet
Dim rngDB As Range, rngF As Range
Dim strAddress As String, n As Long
Dim vR()
Set Ws = Sheets("Sheet1")
With Ws
Set rngDB = .Range("a1", .Range("a" & Rows.Count).End(xlUp))
End With
With rngDB
Set rngF = .Find("Cat", after:=.Cells(.Rows.Count), LookIn:=xlValues, Lookat:=xlWhole)
If rngF Is Nothing Then
Else
strAddress = rngF.Address
Do
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = rngF.Row
Set rngF = .FindNext(rngF)
Loop While strAddress <> rngF.Address
End If
End With
MsgBox WorksheetFunction.Max(vR)
MsgBox vR(n)
End Sub
答案 1 :(得分:0)
你只需要移动你的&#34;&#34;到你要搜索的列的末尾。
在我的例子中,我刚刚找到了A列的最后一行
Dim Last_Index_F As Long
With Sheets("Sheet1")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Last_Index_F = Range("A:A").Find(what:="Cat", after:=Range("A" & LastRow), searchdirection:=xlPrevious).Row
End With
MsgBox (Last_Index_F)