2016-07-11 99 views
0

關閉用戶窗體時出現錯誤(對象變量或未設置塊變量)提示。vba userform運行時錯誤91對象變量或未設置塊變量

Private Sub UserForm_Initialize() 
    TextboxTotal.Text = Worksheets("Data_Base").Range("F2") 
expence.Show 
End Sub 

Private Sub CommandButton1_Click() 
Dim blankrow As Integer 
Dim ws As Worksheet 
Set ws = Worksheets("Data_Base") 
    blankrow = Sheets("Data_Base").Range("A" & Rows.Count).End(xlUp).Row + 1 
    Sheets("Data_Base").Cells(blankrow, 1) = Format(TextDate.Value, "mm/dd/yyyy") 
    Sheets("Data_Base").Cells(blankrow, 2) = ComboBox1 
    Sheets("Data_Base").Cells(blankrow, 3) = Format(TextPrice.Value, "General number") 
    TextboxTotal.Text = Worksheets("Data_Base").Range("F2") 
    TextDate.Value = "" 
    ComboBox1.Value = "" 
    TextPrice.Value = "" 
End Sub 
+0

什麼都行,你讓你的錯誤?你能介入F8嗎? –

+1

另外,忘記詢問用戶表單名稱是否已經存在?如果是這樣,則不需要在'UserForm_Initialize'中添加'expense.show',而是在另一個調用此例程的例程中。 –

回答

0

我猜你遇到在單擊(在右上角的「X」)中的「關閉」按鈕的錯誤,那就是expence您的用戶窗體的名稱。爲了防止用戶關閉用戶窗體與「X」按鈕,在您的用戶窗體的代碼窗格中添加以下代碼

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
    If CloseMode = vbFormControlMenu Then 
     MsgBox "Click the proper button to close the form" '<--| you may want to substitute "proper" with the actual caption of the button you want the user click to exit the userform 
     Cancel = True 
    End If 
End Sub 

而且你已經正確地退出用戶窗體,即通常可以通過完成:具有子負荷,顯示並關閉窗體像如下

  1. Sub main() ' your sub that cals 'expence' userform 
    
        ' ... possible code preceeding 'expence' useform exploitation 
    
        With expence '<--| this loads the Userform and trigger its 'UserForm_Initialize()' event handler, too 
    
         ' ... possible code for some userform controls values initializations not left to 'UserForm_Initialize()' 
    
         .ComboBox1.List = Array(1, 2, 3, 4) 
         .Show '<--| this actually makes the userform visible in the screen 
    
         ' ... possible code for some userform controls values exploitations not already done in its "farewell" event handler ('CommandButton1_Click()' in this case) 
        End With 
        Unload expence '<--| this finally "closes" the userfom 
    
    
    ' ... possible code following 'expence' useform exploitation 
    
    End Sub 
    
  2. 具有useform「告別」子只是隱藏窗體本身

    Private Sub CommandButton1_Click() '<--| thi is tha "farewell" sub, i.e. the one that uses the 'Hide' method of the Userform class to have the user leave the userform 
        Dim blankrow As Long '<--| better use "Long" type variables instead of integers and handle row index greater that 32k or so 
        Dim ws As Worksheet: Set ws = Worksheets("Data_Base") 
    
        blankrow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 
        With Me '<--| "me" actually refers to the Useform itself. this way you benefit from 'Intellisense' and have your control names available after typing the "dot" (".") 
         ws.Cells(blankrow, 1) = Format(.TextDate.Value, "mm/dd/yyyy") 
         ws.Cells(blankrow, 2) = .ComboBox1 
         ws.Cells(blankrow, 3) = Format(.TextPrice.Value, "General number") 
         .TextboxTotal.Text = ws.Range("F2") 
         .TextDate.Value = "" 
         .ComboBox1.Value = "" 
         .TextPrice.Value = "" 
    
         .Hide '<--| this just hides the userfom from the screen, leaving its actual "closing" to the caller sub 
        End With 
    End Sub 
    
+0

@SandeepBhatt,你試過這個嗎? – user3598756

相關問題