vba userform运行时错误91对象变量或未设置块变量

时间:2016-07-11 05:43:27

标签: vba userform

关闭userform时会出现错误(对象变量或未设置块变量)。

 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

1 个答案:

答案 0 :(得分:1)

我猜你在点击"关闭"时遇到了这个错误。按钮(" 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

此外,您必须正确退出userform,通常可以通过以下方式完成:

  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. 有使用形式&#34;告别&#34; sub只是隐藏用户表单本身

    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