Userform Closing Excel

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
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
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
do you have another workbook open?
First time it happened, yes.

Second time I tried, no.

Repeated attempts, no.

The userform should call another userform (#2) when I hit the CMD ("Next") button, not close excel....
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,262
Office Version
2007
Platform
Windows
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
 

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
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.
 

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
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
 

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
Nevermind I'm being an idiot this morning....duhhhh

Query Close will do the trick......thanks!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,262
Office Version
2007
Platform
Windows
Do not worry, sometimes it happens to all of us
:LOL:
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,819
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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!
 

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
618
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,096,319
Messages
5,449,676
Members
405,574
Latest member
Masimo85

This Week's Hot Topics

Top