【发布时间】:2019-02-08 10:25:20
【问题描述】:
我设置了一个宏来向图表添加框并将它们链接到另外两个工作表中的表格。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Declare and assign values to sh1 and sh2
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ActiveWorkbook.Sheets("Training Master")
Set sh2 = ActiveWorkbook.Sheets("Attendance Master")
'Declare and assign values to sp1 and sp2
Dim sp1 As Range
Dim sp2 As Range
Set sp1 = sh1.Range("C:C").Find("2nd Process")
Set sp2 = sh2.Range("C:C").Find("2nd Process")
'Adds boxes to organizational chart
Target.Borders(xlEdgeBottom).LineStyle = xlDouble
Target.Borders(xlEdgeLeft).LineStyle = xlDouble
Target.Borders(xlEdgeRight).LineStyle = xlDouble
Target.Borders(xlEdgeTop).LineStyle = xlDouble
Target.Offset(1).Borders(xlEdgeBottom).LineStyle = xlDouble
Target.Offset(1).Borders(xlEdgeLeft).LineStyle = xlDouble
Target.Offset(1).Borders(xlEdgeRight).LineStyle = xlDouble
Target.Interior.ColorIndex = 45
Target.Font.ColorIndex = 3
'Adds line to table in Training Master, links appropriate cells to
organizational chart
sp1.Offset(0, -2).Resize(1, 19).Insert
sp1.Offset(-2, -1).Copy sp1.Offset(-1, -1)
sp1.Offset(-2, 9).Copy sp1.Offset(-1, 9)
'Adds line to table in Attendance Master, returns error 91
sp2.Resize(1, 7).Insert 'Debug identifies this line as error 91
sp2.Offset(-2).Copy sp2.Offset(-1)
End Sub
似乎每个变量都被分配了一个值,并且我已经两次和三次检查工作表名称的拼写是否与代码中的引用匹配。为什么我会得到:
错误 91 对象变量或未设置块变量
【问题讨论】:
-
哪一行?可能没有找到您的搜索字词。
-
查看代码中的 cmets,倒数第四行。
-
所以
sp2什么都没有,因此出现错误。 -
如果你以
sh1开头,我强烈建议你使用有意义的变量名,你很快就会以sh23结尾并迷失方向。你为什么不直接使用wsTraining和wsAttendance呢?你的代码真的会从中受益,你可以更容易理解自己的代码,更容易维护,从而减少错误。