我正在尝试搜索包含重复信息条目的列。我想选择该列中最后找到的字符串。我该怎么办?
如果可以的话,请告诉我如何使用Selection.Find方法来完成。
Columns("A:A").Select
Selection.Find(What:="foobar", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
答案 0 :(得分:0)
Sub FindAndSelectAll()
Dim str As String, cll As Range, c As Range
Dim rSearch As Range
Set rSearch = Range("A1").CurrentRegion
With rSearch
For Each cll In rSearch
Set c = .Find(cll.Value2, _
LookIn:=xlValues, _
searchdirection:=xlPrevious)
If Not c Is Nothing And cll.Row < c.Row Then
Range(c.Address).Interior.Color = 65535
End If
Next
End With
End Sub
答案 1 :(得分:0)
我找到了想要的答案。通过将SearchDirection属性设置为xlPrevious,它将找到该范围内的最后一个值。然后,可以使用.Select方法将单元格聚焦。但是,如果要搜索的值是该列中其他值的子字符串,则它可能选择了错误的单元格。这可以通过使用while循环来解决,该循环检查它找到的单元格的值,如果不是我们要查找的字符串,则继续搜索先前的值。
Sub FindLast()
Dim fc As Range
Dim my_var As String
Dim cell_check As Variant
my_var = "String 1"
Set fc = Worksheets("Sheet1").Columns("A").Find(what:=my_var, _
SearchDirection:=xlPrevious)
fc.Select
cell_check = ActiveCell.Value
While cell_check <> my_var
Set fc = Worksheets("Sheet1").Columns("A").FindPrevious(after:=fc)
fc.Select
cell_check = ActiveCell.Value
Wend
End Sub
我通过阅读Microsoft网站上的文档找到了解决方案。 https://docs.microsoft.com/en-us/office/vba/api/excel.range.findprevious