【问题标题】:VBA .FindNext while setting other object in loop failingVBA .FindNext 同时在循环中设置其他对象失败
【发布时间】:2017-10-31 12:47:35
【问题描述】:

我正在尝试使用 .FindNext 方法执行 do-while 循环以在列中查找多个实例。就其本身而言,它工作正常。 然而。当试图在同一循环内的另一张表中查找相应的值时,它会抛出“对象变量或未设置块变量”错误 (91)。

If Not found_title Is Nothing Then

    first_cell_address = found_title.Address

    Do
        Set found_url = url_lookup.Find(found_title.Offset(0, -4).Value)
        Debug.Print ("looped")
        Set found_title = title_lookup.FindNext(found_title)

    Loop While first_cell_address <> found_title.Address

End If

当我注释掉 'set found_url...' 时它工作正常,但是当包含它时,'found_title' 对象在它第一次达到 while 条件时被设置为空。由于 FindNext 方法环绕该范围,我不明白为什么在必须至少有一个方法首先输入 if 语句时将其设置为空。

解决方案:

根据 rory 的回答,变化是:

Set found_title = title_lookup.Find(title, After:=Range(found_title.Address))

【问题讨论】:

    标签: vba excel do-while


    【解决方案1】:

    如果您将一个Find 嵌套在另一个中,则不能在外部使用FindNext。您必须重复原来的Find 并将After 参数指定为先前找到的单元格。

    【讨论】:

    • 谢谢,问题解决了。我从不认为“查找”方法本身是嵌套的。
    猜你喜欢
    • 2023-03-21
    • 2012-06-14
    • 2013-03-01
    • 2020-07-07
    • 1970-01-01
    • 2013-06-06
    • 2016-02-26
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多