Excel运行时错误91,没有对象variables

我收到一个Run-time Error '91' Object Variable or With Block not Set for下面的代码。

我很困惑,因为我没有使用任何对象variables(我不认为,我是VBA的新手)。 debugging突出显示了第6行。

我的代码是:

 Dim i As Integer i = 1 Columns("A:A").Select Selection.Find(What:="" & Cells(i, 19).Value, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate MsgBox ActiveCell.Row 

我收到一个运行时错误'91'对象variables或With Block not set for下面的代码。

当使用.Find ,检查是否有匹配返回,然后显示该行,如果发现否则你会得到上述错误。

尝试这个

 Option Explicit Sub Sample() Dim i As Integer Dim aCell As Range Dim ws As Worksheet i = 1 '~~> Replace this with the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Columns(1).Find(What:=.Cells(i, 19).Value, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) '~~> Check if the find returned a match If Not aCell Is Nothing Then MsgBox aCell.Row Else MsgBox "Not Found" End If End With End Sub