2014-11-22 88 views
0

我的Excel 2007項目中的用戶窗體(UF_Main)通過單擊紅色X或跟在Unload Me聲明之後卸載時遇到錯誤Object variable or With block variable not set錯誤。關閉用戶窗體時未設置對象變量

我已經設法錯誤隔離下降到下面的代碼,其是在一個模塊:

Public Sub fProgressExit() 

intProVol = 0 
intProStep = 0 
Unload UF_Progress 
UF_Main.Show 'This line is causing the error' 

End Sub 

卸下線防止錯誤的發生。

卸載用戶表單UF_Main時,沒有提及上述過程,並且該過程涉及的用戶表單(UF_Progress)已被卸載。

爲什麼我在用戶窗體關閉時沒有執行fProgressExit時收到錯誤?

+0

可能UF_Main取決於您項目的參考結構,與UF_Progress一起卸載,因此無法顯示。如果您的需要是使UF_Progress在顯示UF_Main之前消失,請先嚐試UF_Progress.Hide並稍後卸載它。 – 2014-11-22 11:36:36

+0

@MatteoNNZ謝謝,我只是試圖隱藏userform而不是卸載,但它仍然會拋出錯誤。 – Gareth 2014-11-22 11:50:19

+2

而不是鏈接表單,使用標準模塊(例如)中的監督例程來管理表單。使用模式'Dim ufM as UF_Main:set ufM = New UF_Main:ufM.Show'正確聲明它們。這樣你就可以參考表格並完全控制它們的生命週期。 – 2014-11-22 11:52:20

回答

0

如果你不想形式關閉,則可以使用UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)的屬性:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'before unloaded 
Select Case CloseMode 
    Case 0: 'Close initiated by the little [X] button top right of the UserForm 
     'To prevent closing this way : cancel=true 
    Case 1: 'close initiated by Unload UserForm 
    Case 2, 3: 'close initiated by windows(2)/Task manager(3) 
End Select 
End Sub 

需要注意的是:UserForm_QueryClose' is activated before the Form closes, UserForm_Terminate`被激活表單已關閉+卸載後。