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.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,889
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

Board Regular
Joined
Dec 30, 2018
Messages
203
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

Board Regular
Joined
Dec 30, 2018
Messages
203
You're welcome!
You're right I misspelt it, of course, should be vbFormControlMenu :)
 

Forum statistics

Threads
1,082,310
Messages
5,364,428
Members
400,802
Latest member
RichBRich

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top