Userform Cancel Button - Click No

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi for some reason I just can't code a cancel button's 'No' response to simply return to the userform the user was on when the button was clicked.

When the 'Yes' response is clicked the form closes and loads the Main form as it is meant to but if I click No then the current form also closes. The code below is what I started with but I've tried various ways to just go back to the original form.

Anyone got any suggestions please?

Thanks Paul

VBA Code:
Private Sub cmbCancel_Click()

        MsgBox "Are you sure you want to cancel this record?" & vbCrLf & _
        "If you select yes, you will lose all the information entered.", vbExclamation + vbYesNo, "Cancel Entry"
            
        Unload Me
        MainList.Show

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Your code is not testing the return value of the msgbox function

Try this update & see if resolves

VBA Code:
Private Sub cmbCancel_Click()

        If MsgBox("Are you sure you want to cancel this record?" & vbCrLf & _
        "If you select yes, you will lose all the information entered.", vbExclamation + vbYesNo, "Cancel Entry") = vbYes Then
            
        Unload Me
        MainList.Show
       End If

End Sub

You can read more on the Msgbox function in VBA helpfile

Dave
 
Upvote 0
Solution
Thanks so much for the reply and the link on msgbox's. I just assumed if the No option was clicked nothing would happen but thats just my inexperience showing. I'll have a read through the link showed as it will help me learn more but the code edit you supplied works perfectly.

Thanks again Paul
 
Upvote 0
most welcome - your appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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