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

gijimbo

Board Regular
Joined
Feb 3, 2010
Messages
128
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.
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

DanteAmor

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

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
307
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. :)
 

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
307
You're welcome!
You're right I misspelt it, of course, should be vbFormControlMenu :)
 

Watch MrExcel Video

Forum statistics

Threads
1,095,290
Messages
5,443,599
Members
405,240
Latest member
spl

This Week's Hot Topics

Top