excel - 运行时错误 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
解决方案
错误发生在这一行: myStrAddress = myAddress.Address – regnerrad 1 分钟前
您收到该错误是因为无法找到代码strTemp
并且因为它myAddress
是Nothing
.
换行
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
推荐阅读
- julia - 如何将权重参数添加到广义混合模型
- swift - 如何将多个项目添加到 UITableViewCell?
- javascript - 在 JavaScript 中编写对多个对象属性进行排序的排序方法的最简洁方法
- php - 使用 Timber Twig 在 Wordpress 中自定义 php 文件目录
- haskell - Haskell中二进制数的加法
- build - 哪种方式更好地创建 deb 文件?
- gtk - 我是否在 gtk_text_buffer_get_text 返回时调用 g_free
- reactjs - 反应本机 AsyncStorage 问题
- c++ - 程序在可区分联合的复制构造函数内崩溃
- android - 带有 Flutter 应用程序的 Visual Studio 应用程序中心