代码之家  ›  专栏  ›  技术社区  ›  Gothic_Anatomist

VBA。在循环中设置其他对象时查找下一个失败

  •  0
  • Gothic_Anatomist  · 技术社区  · 7 年前

    我正在尝试使用执行do while循环。FindNext方法在一列中查找多个实例。就其本身而言,它运行良好。 然而当试图在同一循环内的另一张表中找到相应值时,它抛出“Object variable or With block variable not set”(对象变量或未设置块变量)错误(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\u url…”它工作正常,但当包含它时,“found\u title”对象在第一次达到while条件时已设置为nothing。由于FindNext方法围绕着范围,我不明白为什么在必须至少有一个方法才能首先输入if语句的情况下将其设置为nothing。

    解决方案:

    根据rory的回复,变化是:

    Set found_title = title_lookup.Find(title, After:=Range(found_title.Address))
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Rory    7 年前

    如果你有一个 Find 在另一个内部,你不能使用 FindNext 在外面。你必须重复原文 发现 并指定 After 参数作为上一个找到的单元格。