Exit main macro if frmMsgBox equals no, else run main macro

Rackette

New Member
Joined
Jul 2, 2019
Messages
37
Hello! :)
I have a large main macro. I would like for the very first thing it does is to call a custom msg box that will ask the user whether or not they wish to exit the macro, or if they wish to continue.
If they wish to exit, I need to exit out of the custom msgbox sub AND also out of the main macro.
If they wish to continue, then I need to leave the custom msgbox and continue through the main macro.
This is what I've cobbled together from several sources:

Code:
 Sub frmMsgBoxWCE()Dim myForm As frmCustomMsgBox
  Set myForm = New frmCustomMsgBox
  myForm.Caption = "West Complex  Unit E  Staff Callout"
        myForm.Show
        Select Case myForm.Tag
          Case 1
            GoTo lbl_Exit
          Case 2
          Exit Sub
        End Select
        'End If
  
lbl_Exit:
    Unload myForm
    Set myForm = Nothing
Exit Sub
End Sub

Case 1 (the "I wish to exit") is button 1 on my form.
Case 2 (the "I wish to continue") is button 2 on my form.

I hope this makes sense to someone. I have my fingers crossed...and I'll eat another cookie as moral support.

-Christine
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You could put everything inside the userform code module. This example has a Lable and the two command buttons.

Code:
' in code module for frmCustomMessageBox

Private Sub CommandButton1_Click()
    Rem cancel button
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    Rem OK button
    Me.Tag = "OK"
    Me.Hide
End Sub

Private Sub UserForm_Initialize()
    Rem these can be set at design time (or passed as arguments to the Value property)
    Me.Caption = "West Complex  Unit E  Staff Callout"
    Me.CommandButton1.Caption = "Cancel"
    Me.CommandButton2.Caption = "OK"
End Sub

Public Function Value(Optional Prompt As String) As VbMsgBoxResult
    With Me
        .Label1 = Prompt
        .Show
    End With
    
    With frmCustomMessageBox
        If .Tag = "OK" Then
            Value = vbOK
        Else
            Value = vbCancel
        End If
    End With
    Unload frmCustomMessageBox
End Function

It would be called from the main routine like this

Code:
If frmCustomMessageBox.Value("Shall I Continue?") = vbCancel Then Exit Sub
Note that pressing the corner X is the same as Cancel button.

Also, you can pass other arguments to the Value property depending on how custom you want the message box.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

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