How to detect Userform 'X' or Cancel click from sub that loaded it

gijimbo

Board Regular
Joined
Feb 3, 2010
Messages
130
I have a Sub that loads a userform I created. In the sub, I change a label caption in the form and reuse it using a loop to capture the user's option selection for each new context (but with the same options presented).

Code:
Load frmExample

With ActiveSheet
    For x = 1 To UBound(vValue)
        .Cells(inputRow, vValue(x)).Select
        frmExample.lblParameter.Caption = .Cells(inputRow - 1, vValue(x)).Value
        frmExample.Show
        
        'do things depending on which option button was selected
    Next x
End With

I'd like to know how to tell the Sub that the 'X' close button was clicked so it doesn't continue to re-display the Userform.

I know the UserForm_QueryClose() Sub runs when the X is clicked but I don't know how to use that to tell the "calling" Sub that it was clicked. I tried creating a Public boolean variable in the code section for the userform and setting it to True when QueryClose would run but when it would return to the "calling" Sub, the variable wouldn't remain set to True.
 

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
Try this:

In your Sub:
Code:
Public wQuit As Boolean     'at the beginning of all the code


Sub test()
  '
  Load frmExample
  With ActiveSheet
    For x = 1 To UBound(vvalue)
      .Cells(inputRow, vvalue(x)).Select
      frmExample.lblParameter.Caption = .Cells(inputRow - 1, vvalue(x)).Value
      If wQuit = False Then
        frmExample.Show
      Else
        Exit For
      End If
      'do things depending on which option button was selected
    Next x
  End With
End Sub

In your userform:
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  If CloseMode = 0 Then wQuit = True
End Sub
 
Upvote 0
Hi
Declare public parameter
Publuc bool_ClosedByControl as boolean (declare it above the sub!)
At the beginning of your sub, before for statement set it to false, bool_ClosedByControl=false
Then, open code of your form, select event on the form called QuerryClose and type in there the following code
Code:
If closemode=vbclosecontrolmenu then
   bool_ClosedByControl=true
Else
   bool_ClosedByControl=false
End if

Then, based on the value bool_ClosedByControl you can show or not show the form
Code:
If bool_ClosedByControl = false then frmExample.Show
Let me know if that helps you. :)
 
Upvote 0
You're welcome!
You're right I misspelt it, of course, should be vbFormControlMenu :)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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