当我关闭我正在使用的工作簿时,我试图清除不同工作簿中两个单元格中的值。当我关闭我的工作簿时,我收到以下错误:
运行时错误' 91':对象变量或未设置块变量
使用调试和单步执行我可以找到收到错误的行,但对于我的生活,我没有看到我错过的内容。
我打电话给我的宏:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
TestClear
End Sub
Sub TestClear()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim SourceWb As Workbook, myWS As Worksheet
Set SourceWb = Workbooks.Open("Workbook file location, in this case it's on a network share")
Set myWS = SourceWb.Sheets("Sheet1") <---- Here is where I receive the Run-Time error '91'
myWS.Range("A1").Clear
myWS.Range("B1").Clear
SourceWb.Close SaveChanges:=1
ThisWorkbook.Close SaveChanges:=1
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
答案 0 :(得分:0)
尝试运行以下代码,直到您可以执行此操作而不会出现错误:
Sub TestClear()
Dim SourceWb As Workbook
Dim myWS As Worksheet
Set SourceWb = ThisWorkbook
Set myWS = SourceWb.Worksheets("Sheet1")
Debug.Print myWS.Name
End Sub
错误可能与您使用SourceWb
的方式有关,或者您没有Sheet1
。
答案 1 :(得分:0)
你是一个可重入,理论上永无止境的例程的受害者。 Workbook_BeforeClose
内的没有停用事件,您调用ThisWorkbook.Close
,这将再次导致Workbook_BeforeClose
被调用,然后一次又一次地调用Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False '<------- To avoid re-entance
On Error Resume Next
TestClear
Application.EnableEvents = True
End Sub
。这会导致未定义的行为(可能是 StackOverfow !)
快速修复:禁用事件:
fig3,axes = plt.subplots(ncols=2,nrows=5)
fig3.tight_layout()
plt.subplots_adjust(hspace = 0.8,top = 0.9 )
A.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[0,0])
axes[0][0].axvline(indices.loc[0,Indice],color='b')
axes[0][0].axvline(indices.loc[1,Indice],color='b')
axes[0][0].axvline(indices.loc[2,Indice],color='k')
axes[0][0].axvline(indices.loc[3,Indice],color='k')
axes[0][0].axvline(indices.loc[4,Indice],color='r')
axes[0][0].axvline(indices.loc[5,Indice],color='r')
axes[0][0].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
B.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[1,0])
axes[1][0].axvline(indices.loc[0,Indice],color='b')
axes[1][0].axvline(indices.loc[1,Indice],color='b')
axes[1][0].axvline(indices.loc[2,Indice],color='k')
axes[1][0].axvline(indices.loc[3,Indice],color='k')
axes[1][0].axvline(indices.loc[4,Indice],color='r')
axes[1][0].axvline(indices.loc[5,Indice],color='r')
axes[1][0].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
C.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[2,0])
axes[2][0].axvline(indices.loc[0,Indice],color='b')
axes[2][0].axvline(indices.loc[1,Indice],color='b')
axes[2][0].axvline(indices.loc[2,Indice],color='k')
axes[2][0].axvline(indices.loc[3,Indice],color='k')
axes[2][0].axvline(indices.loc[4,Indice],color='r')
axes[2][0].axvline(indices.loc[5,Indice],color='r')
axes[2][0].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
D.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[3,0])
axes[3][0].axvline(indices.loc[0,Indice],color='b')
axes[3][0].axvline(indices.loc[1,Indice],color='b')
axes[3][0].axvline(indices.loc[2,Indice],color='k')
axes[3][0].axvline(indices.loc[3,Indice],color='k')
axes[3][0].axvline(indices.loc[4,Indice],color='r')
axes[3][0].axvline(indices.loc[5,Indice],color='r')
axes[3][0].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
E.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[4,0])
axes[4][0].axvline(indices.loc[0,Indice],color='b')
axes[4][0].axvline(indices.loc[1,Indice],color='b')
axes[4][0].axvline(indices.loc[2,Indice],color='k')
axes[4][0].axvline(indices.loc[3,Indice],color='k')
axes[4][0].axvline(indices.loc[4,Indice],color='r')
axes[4][0].axvline(indices.loc[5,Indice],color='r')
axes[4][0].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
F.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[0,1])
axes[0][1].axvline(indices.loc[0,Indice],color='b')
axes[0][1].axvline(indices.loc[1,Indice],color='b')
axes[0][1].axvline(indices.loc[2,Indice],color='k')
axes[0][1].axvline(indices.loc[3,Indice],color='k')
axes[0][1].axvline(indices.loc[4,Indice],color='r')
axes[0][1].axvline(indices.loc[5,Indice],color='r')
axes[0][1].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
G.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[1,1])
axes[1][1].axvline(indices.loc[0,Indice],color='b')
axes[1][1].axvline(indices.loc[1,Indice],color='b')
axes[1][1].axvline(indices.loc[2,Indice],color='k')
axes[1][1].axvline(indices.loc[3,Indice],color='k')
axes[1][1].axvline(indices.loc[4,Indice],color='r')
axes[1][1].axvline(indices.loc[5,Indice],color='r')
axes[1][1].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
H.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[2,1])
axes[2][1].axvline(indices.loc[0,Indice],color='b')
axes[2][1].axvline(indices.loc[1,Indice],color='b')
axes[2][1].axvline(indices.loc[2,Indice],color='k')
axes[2][1].axvline(indices.loc[3,Indice],color='k')
axes[2][1].axvline(indices.loc[4,Indice],color='r')
axes[2][1].axvline(indices.loc[5,Indice],color='r')
axes[2][1].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
I.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[3,1])
axes[3][1].axvline(indices.loc[0,Indice],color='b')
axes[3][1].axvline(indices.loc[1,Indice],color='b')
axes[3][1].axvline(indices.loc[2,Indice],color='k')
axes[3][1].axvline(indices.loc[3,Indice],color='k')
axes[3][1].axvline(indices.loc[4,Indice],color='r')
axes[3][1].axvline(indices.loc[5,Indice],color='r')
axes[3][1].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
J.plot(x=0,y=1, linewidth=0.4,color='k',legend=False,ax=axes[4,1])
axes[4][1].axvline(indices.loc[0,Indice],color='b')
axes[4][1].axvline(indices.loc[1,Indice],color='b')
axes[4][1].axvline(indices.loc[2,Indice],color='k')
axes[4][1].axvline(indices.loc[3,Indice],color='k')
axes[4][1].axvline(indices.loc[4,Indice],color='r')
axes[4][1].axvline(indices.loc[5,Indice],color='r')
axes[4][1].set_xlim([indices.loc[0,Indice]-10,indices.loc[5,Indice]+10])
axes[4][1].set_ylim(-.5,2)
plt.show()