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

gijimbo

Board Regular
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
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
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
You're welcome!
You're right I misspelt it, of course, should be vbFormControlMenu :)
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top