【问题标题】:VBA .findnext not working. Runtime error 91 object variable or with block variable not setVBA .findnext 不工作。运行时错误 91 对象变量或未设置块变量
【发布时间】:2013-06-06 02:54:17
【问题描述】:

我正在尝试执行搜索,它在列中搜索“REQM”(无引号)并将找到的单元格的范围设置为 d。然后调用另一个子函数来查找输入数据的位置。我的 FindEntryArea 子函数运行良好,我的第一个 find 运行良好,但是当它尝试 findnext 时却无法正常运行。

Sub FindLoop()
Dim re as Range
Set re = Sheets(1).Range("T:T")

With re
    Set d = .Find("REQM", LookIn:=xlFormulas, LookAt:=xlWhole)
    MsgBox (d.Row)
    Call FindEntryArea
    Do
        Set d = .FindNext(d)
        MsgBox (d.Row)
        Call FindEntryArea
    Loop While Not d Is Nothing
End With


End Sub

试图找出错误,我使用 msgbox 打印出找到的范围行,这对第一个单元格有效,但对 findnext 无效。我得到对象变量或未设置块变量。我对 VBA 相当陌生,这是我第一次使用 findnext,因此将不胜感激。还有 re 是我的范围,还有很多其他的单元格应该在其中找到。

谢谢。

编辑:

主代码和查找循环

Public re As Range
Public d As variant
Sub MainCode()

Dim r as Range
Set re = Worksheets("Summary all PIIDB").Range("T:T")

Set r = Worksheets("Summary all PIIDB")
With r
    Call FindLoop
End With
End Sub

Sub FindLoop()

With re
    Set d = .Find("REQM", LookIn:=xlFormulas, LookAt:=xlWhole)
    MsgBox (d.Row)
    'Call FindEntryArea
        Set d = .FindNext(d)
        MsgBox (d.Row)
        'Call FindEntryArea
End With


End Sub

我删除了循环只是为了让 findnext 先工作,但我仍在苦苦挣扎。

【问题讨论】:

    标签: vba find range runtime-error with-statement


    【解决方案1】:

    问题是您从未将变量“re”或“c”设置为任何值。您确实应该在使用它们之前声明所有变量以帮助减少错误。试试这样的:

    Sub FindLoop()
        Dim prevSheet as Worksheet
        Dim rng As Range
        Dim fnd As Variant
        Dim i As Long
    
        prevSheet = ActiveSheet
        Sheets(1).Select
    
        'Column T - UsedRange
        Set rng = Sheets(1).Range(Cells(1, 20), Cells(ActiveSheet.UsedRange.Rows.Count, 20))
    
        On Error GoTo Not_Found
        i = rng.Find("REQM", LookIn:=xlFormulas, LookAt:=xlWhole).Row
        On Error GoTo 0
    
        With rng
            Set fnd = .Find("REQM", LookIn:=xlFormulas, LookAt:=xlWhole)
            Do
                Set fnd = .FindNext(fnd)
                Call FindEntryArea
                MsgBox (fnd.Row)
            Loop While i < fnd.Row
        End With
        prevSheet .select
    
        Exit Sub
    
    Not_Found:
        MsgBox """REQM"" not found."
        prevSheet.Select
        Exit Sub
    End Sub
    

    编辑: 我修改了您发布的代码,它对我来说可以正常运行。

    Option Explicit
    Public d As Variant
    Public re As Range
    
    Sub MainCode()
    
        Dim r As Range
        Set re = Worksheets("Summary all PIIDB").Range("T:T")
    
        Set r = Worksheets("Summary all PIIDB").UsedRange
        With r
            Call FindLoop
        End With
    End Sub
    
    Sub FindLoop()
        On Error GoTo Not_Found
        With re
            Set d = .Find("REQM", LookIn:=xlFormulas, LookAt:=xlWhole)
            MsgBox (d.row)
            'Call FindEntryArea
            Set d = .FindNext(d)
            MsgBox (d.row)
            'Call FindEntryArea
        End With
        On Error GoTo 0
        Exit Sub
    
    Not_Found:
        MsgBox ("REQM not found!")
        Exit Sub
    End Sub
    

    【讨论】:

    • Loop While Not c Is Nothing,"c" 似乎从未被定义。
    • 我确实将 re 设置为公共范围,并将其设置在我未显示的程序的不同部分中。我发布的只是一个程序。该范围有效,因为我得到了第一个 .find 完美但下一个从未设置。
    • 这是我的另一个错误,但我尝试修复它,但我仍然收到错误。由于其他原因,d 没有设置 .findnext。
    • 使用 F8 单步执行代码表明 .FindNext 语句确实执行正确。我将“d”声明为一个变体。我不知道您将它设置为什么数据类型,因为它从未在您的代码中声明。
    • 问题解决了!我没有意识到在另一个过程中使用不同的查找会弄乱我原来的查找。因此,我没有使用 findnext,而是使用了另一个 find 并将 after 参数设置为在原始 find 之后查找。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多