VBA best practices for modules relative to modeless userforms

1.8k views Asked by At

I came across this similar issue and read the replies: Modeless form that still pauses code execution

I have been attempting to apply in my own situation the suggestion provided by David Zemens. In my situation, I cannot seem to find an approach that incorporates Mr. Zemen's suggestion without also utilizing a GoTo.

I am wondering if there is a better or more elegant solution.

Here is an outline of what I am doing:

I have a UserForm with a Command Button that begins the code execution that will perform several actions on multiple Excel workbooks. As such, there are a number of blocks of code and the successful completion of one block of code allows for the execution of the subsequent block of code.

At a certain point, depending on the situation, the code might require User input; in other situations, the needed data is obtainable from an Excel. If input is needed from the User, another UserForm is displayed.

The User may need to view several different Excel sheets before entering the input, so the UserForm is modeless. So the code comes to a stop until the User enters the needed input and clicks another Command Button.

It is at this point I am having trouble: how to resume the program flow. Is the only way to 'pick-up where it left-off' is by using a GoTo statement? Or is there some way to organize the modules so there is a single consistent program flow, defined in one spot and not duplicated from the point at which User input might be needed?

2

There are 2 answers

0
cyboashu On

Here is my take on the problem . Hope I understood the problem correctly.

Assumptions:

  1. There are two user forms.
  2. UserForm1 with a button to start the processing.
  3. UserForm2 with a button to supply intermediate input.
  4. A sub inside a module to start/ launch UserForm1.

VBA Code (for the sub routine)

Sub LaunchUserForm1()
    Dim frm As New UserForm1

    '/ Launch the main userform.
    frm.Show vbModeless
End Sub

VBA Code (for UserForm1)

Private Sub cmdStart_Click()
    Dim i       As Long
    Dim linc    As Long
    Dim bCancel As Boolean
    Dim frm     As UserForm2

    '/ Prints 1 to 5 plus the value returned from UserForm2.

    For i = 1 To 5

        If i = 2 Then
            Set frm = New UserForm2
            '/ Launch supplementary form.
            frm.Show vbModeless

'<< This is just a PoC. If you have large number of inputs, better way will be
' to create another prop such as Waiting(Boolean Type) and then manipulate it as and when User
' supplies valid input. Then validate the same in While loop>>

            '/ Wait till we get the value from UserForm2.
            '/ Or the User Cancels the Form with out any input.               
            Do While linc < 1 And (linc < 1 And bCancel = False)
                linc = frm.Prop1
                bCancel = frm.Cancel
                DoEvents
            Loop

            Set frm = Nothing
        End If

        Debug.Print i + linc
    Next

    MsgBox "User Form1's ops finished."

End Sub

VBA Code (for UserForm2)

Dim m_Cancel        As Boolean
Dim m_prop1         As Long

Public Property Let Prop1(lVal As Long)
    m_prop1 = lVal
End Property

Public Property Get Prop1() As Long
   Prop1 = m_prop1
End Property

Public Property Let Cancel(bVal As Boolean)
    m_Cancel = bVal
End Property

Public Property Get Cancel() As Boolean
    Cancel = m_Cancel
End Property

Private Sub cmdlinc_Click()
    '/Set the Property Value to 10
    Me.Prop1 = 10
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    '/ Diasble X button

    Me.Cancel = True
    Me.Hide
    Cancel = True

End Sub
7
David Zemens On

OK so here are my thoughts.

You have a userform frmSelectUpdateSheet which you wish to use in order to allow the user to select the sheet, when the sheet can't be determined programmatically. The problem is that if you do .Show vbModeless (which allows the user to navigate the worksheet/s), then code continues to execute which either leads to errors or otherwise undesired output.

I think it's possible to adapt the method I described in the previous answer. However, that's out of the question here unless you're paying me to reverse engineer all of your code :P

Assuming you have a Worksheet object variable (or a string representing the sheet name, etc.) which needs to be assigned at this point (and that this variable is Public in scope), just use the CommandButton on the form to assign this based on the selected item in the frmSelectUpdateSheet list box.

This is probably a superior approach for a number of reasons (not the least of which is trying to avoid application redesign for this sort of fringe case), such as:

  • This keeps your form vbModal, and does prevent the user from inadvertently tampering with the worksheet during the process, etc.

  • Using this approach, the thread remains with the vbModal displayed frmSelectUpdateSheet, and you rely on the form's event procedures for control of process flow/code execution.

  • It should be easier (and hence, cheaper) to implement; whether you're doing it yourself or outsourcing it.

  • It should be easier (and hence, cheaper) to maintain.

NOW, on closer inspection, it looks like you're already doing this sort of approach with the cmdbtnSelect_Click event handler, which leads me to believe there's a related/follow-up problem:

The sheet names (in listbox) are not sufficient for user to identify the correct worksheet. So if the user needs the ability to "scroll" the sheet (e.g., to review data which does not fit in the window, etc.), then add some spinner buttons or other form controls to allow them to navigate the sheet.