gpt4 book ai didi

excel - .FindNext() 与 xlPrevious

转载 作者:行者123 更新时间:2023-12-04 22:30:13 27 4
gpt4 key购买 nike

我试图从下到上查找所有值“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 .

我不确定这是从下到上查找所有值的最佳方法。任何帮助将不胜感激。

最佳答案

Range.FindNext method显然没有继承 SearchDirection:=xlPrevious 参数。您需要底座 Range.Find method用 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

关于excel - .FindNext() 与 xlPrevious,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com