Userform Closing Excel

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Hello,

I have a userform that has decided that it is going to close excel each time I use it. I'm not sure where I've gone wrong in my coding and was hoping someone could double check it for me.

thanks!

Code:
 Private Sub CommandButton5_Click()'"next" page button
On Error GoTo Helper
'Application.Visible = False
    Unload Me
    UserForm2.Show


'Error Clearing Code
Exit Sub
Helper:
    resp = MsgBox("We're sorry to see you've encountered an error." & vbCrLf & vbCrLf & "To proceed, we recommend you contact the Developer " & _
    "with error codes [1044] and " & "[" & Err.Number & "-" & Err.Description & "]." & vbCrLf & vbCrLf & "To attempt to patch your problem at least " & _
    "temporarily, we recommend you click [Yes] to see help directions. Would you like to continue?", vbYesNoCancel, name)
        If resp = vbYes Then
            UserForm18.Show
            'MsgBox ("Success")
        ElseIf resp = vbNo Then
            Exit Sub
        ElseIf resp = vbCancel Then
            Exit Sub
        End If
End Sub

Private Sub UserForm_Terminate()
On Error GoTo Helper
Unload Me
            'Application Closer
                If Workbooks.Count > 1 Then
                    ActiveWorkbook.Close
                Else: Application.Quit
                End If
'Error Clearing Code
Exit Sub
Helper:
    resp = MsgBox("We're sorry to see you've encountered an error." & vbCrLf & vbCrLf & "To proceed, we recommend you contact the Developer " & _
    "with error codes [1060] and " & "[" & Err.Number & "-" & Err.Description & "]." & vbCrLf & vbCrLf & "To attempt to patch your problem at least " & _
    "temporarily, we recommend you click [Yes] to see help directions. Would you like to continue?", vbYesNoCancel, name)
        If resp = vbYes Then
            UserForm18.Show
            'MsgBox ("Success")
        ElseIf resp = vbNo Then
            Exit Sub
        ElseIf resp = vbCancel Then
            Exit Sub
        End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
In this part

Code:
Private Sub UserForm_Terminate()
On Error GoTo Helper
Unload Me
            'Application Closer
                If Workbooks.Count > 1 Then
                    ActiveWorkbook.Close
                Else: Application.Quit
                End If
'Error Clearing Code
Exit Sub


If the active book is the book of the form, then the book is closed.
Code:
If Workbooks.Count > 1 Then
                    ActiveWorkbook.Close

If there is more than one book, then you close excel
Code:
Else: Application.Quit
 
Upvote 0
Yes, but I wrote that thinking that if the userform was closed (i.e. hitting the "x") it would close the workbook. The CMD button code (a "Next" button) should take the user to the next userform and unload the current.
 
Upvote 0
ahhh ok so now I see your point! thank you!

So question... If I wanted the "x" for the userform to close the workbook, what would that code look like- basically I don't want the user to get into the workbook if they close the userform (because the userform requires a "password" to proceed and closing the userform gets past the password!).

Thanks
 
Upvote 0
Do not worry, sometimes it happens to all of us
:LOL:
 
Upvote 0
Just as an aside, there is no point in having Unload Me in the Terminate event. It's already unloading if that event is firing!
 
Upvote 0
Yep....i'm not even sure how I came to write the terminate event in there....I was shooting myself in the foot...I'm guessing it was one of my late-night sessions.

Thanks!

On a side note- anyone have ideas on if I can get a userform to display an error code from another module?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top