0

An error(object variable or with block variable not set) prompts when I close 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
2
  • at what line are you getting your error ? can you step in with F8 ? Commented Jul 11, 2016 at 5:51
  • 1
    Also, forgot to ask if the User Form name is expence ? if so you don't need to add the expense.show in the UserForm_Initialize, but at another routine that calls the this routine. Commented Jul 11, 2016 at 5:57

1 Answer 1

1

I guess you're having that error upon clicking the "Close" button (the "X" in the upper-right corner), and that expence is the name of your userform.

in order to prevent the user closing the userform with the "X" button add the following code in your userform code pane

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

furthermore you have to properly exit the userform, that can be usually done by:

  1. having the parent sub load, show and close the userform like follows:

    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. having the useform "farewell" sub just hide the userform itself

    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
    
Sign up to request clarification or add additional context in comments.

1 Comment

@SandeepBhatt, did you try this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.