我试图从下到上查找所有值“ O”。我无法使用“正常”查找,因为我要向下offset
找一些值,而.Find()
将再次找到它们。
问题在于,当我在.FindNext
和.Find()
之后使用SearchDirection:=xlPrevious
时,它会返回顶部,而不是遵循方向。
出于测试目的,我使用以下代码:
Dim f As Range
Set ws = ThisWorkbook.ActiveSheet
ws.Cells(7, 5).Value = "O"
ws.Cells(6, 5).Value = "O"
ws.Cells(5, 5).Value = "O"
ws.Cells(5, 6).Value = "O"
ws.Cells(5, 7).Value = "O"
Set f = ws.Range("A1:AX50").Find(what:="O", SearchDirection:=xlPrevious)
Debug.Print f.Address
Set f = ws.Range("A1:AX50").FindNext(f)
Debug.Print f.Address
它打印$E$7
和$E$5
。我期待着$E$7
和$E$6
。
我不确定这是从下至上查找所有值的最佳方法。任何帮助将不胜感激。
答案 0 :(得分:2)
Range.FindNext method显然没有继承SearchDirection:= xlPrevious参数。您需要使用{:3}}和After:= f作为基础。
Dim f As Range, addr As String, ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
ws.Cells(7, 5).Value = "O"
ws.Cells(6, 5).Value = "O"
ws.Cells(5, 5).Value = "O"
ws.Cells(5, 5).Value = "O"
ws.Cells(5, 6).Value = "O"
ws.Cells(5, 7).Value = "O"
With ws.UsedRange.Cells 'Range("A1:AX50")
Set f = .Find(What:="O", after:=.Cells(1), SearchDirection:=xlPrevious, SearchOrder:=xlByRows)
If Not f Is Nothing Then
addr = f.Address(0, 0)
Do
Debug.Print f.Address
Set f = .Find(What:="O", after:=f, SearchDirection:=xlPrevious, SearchOrder:=xlByRows)
Loop Until addr = f.Address(0, 0)
End If
End With
答案 1 :(得分:0)
使用 .FindPrevious 而不是 .FindNext