首页 > 解决方案 > 运行时错误 91:即使我根本没有更改我的代码,也没有设置对象变量或带有块变量

问题描述

我写了这段代码,它以前运行良好,但不知何故突然出现了这个错误。

因此,根据我下面的代码,我查看了 MyAddress,即使该范围内有要查看的实际数据,它也没有。

For m = LBound(varRngtoFormat, 1) To UBound(varRngtoFormat, 1)
    Set ws1 = wb1.Worksheets(varRngtoFormat(m, 1))
    lngTemp = xLastRow(ws1)
    Set strRange = ws1.Range("C5:C" & lngTemp)
    Set strRange1 = ws1.Range("G5:G" & lngTemp)
    Set strRange2 = ws1.Range("f5:F" & lngTemp)
On Error Resume Next
    For Each rngTemp In strRange
        colTemp.add rngTemp, rngTemp
    Next rngTemp
    On Error GoTo 0

    If colTemp.Count > 0 Then
        For i = 1 To colTemp.Count
            strTemp = colTemp(i)
            dblTemp = 0
            Debug.Print strTemp
            For Each rngTemp In strRange
                If rngTemp = strTemp Then
                    dblTemp = dblTemp + rngTemp.RowHeight
                    Debug.Print dblTemp
                End If
            Next rngTemp
            Set myAddress = strRange.Find(what:=strTemp, LookIn:=xlValues, lookat:=xlWhole)
            **myStrAddress = myAddress.Address**
            With ws1
                myLeft = .Columns("A").Left
                myWidth = .Columns("A").Width
                myHeight = dblTemp
                myTop = .Range(myStrAddress).Top
                With .Shapes.AddShape(msoShapeRoundedRectangle, myLeft, myTop, myWidth, myHeight)
                    .TextFrame.Characters.Text = strTemp
                    .TextFrame2.VerticalAnchor = msoAnchorMiddle
                    .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
                    .TextFrame.Characters.Font.Bold = True
                    .TextFrame.Characters.Font.Size = 16
                    .Fill.ForeColor.RGB = RGB(0, 0, 0)
                    .Line.Visible = False
                    .Line.Weight = 1.5
                    .Line.ForeColor.RGB = RGB(255, 255, 255)
                    .Shadow.Type = msoShadow21
                    .Shadow.Size = 0.97
                End With
            End With
        Next i
    End If
    Set colTemp = Nothing
Next m

标签: excelvbaruntime-error

解决方案


错误发生在这一行: myStrAddress = myAddress.Address – regnerrad 1 分钟前

您收到该错误是因为无法找到代码strTemp并且因为它myAddressNothing.

换行

myStrAddress = myAddress.Address

If myAddress Is Nothing Then
    MsgBox "Not Found" '<~~ Or whatever message/action you want to show/take
    Exit Sub
Else
    myStrAddress = myAddress.Address
End If

提示:无论何时使用.Find,请始终检查是否.Find通过使用返回任何内容If MyObject Is Nothing


推荐阅读