vba - Excel VBA : How to solve Run-time error '91' ?

标签 vba excel

这个问题在这里已经有了答案:





Keep getting Error 91 with Excel Find function

(1 个回答)


3年前关闭。




我得到错误:
Run-time error '91': Object variable or With block variable not set
当我尝试执行此代码时:

MsgBox Worksheets("Sheet2").Range("2:2").Find(Worksheets("Sheet1").Range("E5").Value, , , xlWhole)

为什么?这一行有什么问题?

编辑

如果我正在管理文本或数字,则该代码有效。但我有这个问题 日期 Worksheets("Sheet1").Range("E5")Worksheets("Sheet2").Range("2:2") .

最佳答案

分配 result到一个范围并使用 .Find()设置范围。
如果没有找到,那么 Range()将是 Nothing :

Sub TestMe()

    Dim result As Range

    With Worksheets(1)
        Set result = .Range("2:2").Find("Something", LookAt:=xlWhole)
    End With

    If Not result Is Nothing Then
        Debug.Print result.Address
    Else
        Debug.Print "Something is not on the second row!"
    End If

End Sub
LookAt:=xlWhole如果使用日期,参数是必须的:
  • Range.Find not making a difference between January and November (February and December) in VBA Excel
  • Excel.Range.Find Documentation
  • 关于vba - Excel VBA : How to solve Run-time error '91' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504282/

    相关文章:

    excel - 如何检测和突出显示单元格中是否有多个单词

    sql - 在VBA中引用查询结果,也许可以使用SQL?

    vba - 根据单元格值复制代码

    excel - 如何打开现有 PowerPoint 图表的数据表

    Excel - 在每行中生成三组数字的笛卡尔积

    vba - 使用 Excel VBA 在命令提示符下执行命令

    perl - Excel::Writer::XLSX 单元格(带有日期和时间)需要刷新

    excel - 链接到基于另一个单元格的外部工作簿

    vba - 位置图表相对于 VBA 中的另一个图表(上方或下方)

    vba - 从 A 列复制范围并写入 B 列