VBA form OK_click not returning to calling Sub

myall_blues

Board Regular
Joined
Nov 26, 2015
Messages
96
Office Version
  1. 365
Platform
  1. Windows
Hi

I have an issue with a Sub that calls a userform - after I click OK on the form it does not continue with the Sub but rather just stops. Debugging seems to indicate that is stays at the 'show' statement in the sub. I have this same code in another application and it works perfectly, and I have been unable to figure out the difference.
Here is my Sub calling code:
VBA Code:
Private Sub ImportData()
    '
    '
    ' Get input filename from form
    '
    Dim frm As New frmGetFile
    With frm
        .Show                                         ' <= Never goes past this line
        If Not .TypeCancelled Then
            '
            ' Do some further processing
            '
        End If
        '
        ' Clean up
        '
        Unload frm
        Set frm = Nothing
    End With
End Sub

And here is the code in my userform:
VBA Code:
Option Explicit
Private cancelled As Boolean
Public Property Get TypeCancelled() As Boolean
    TypeCancelled = cancelled
End Property
Private Sub cmdGetFileCancel_Click()
    '
    ' Cancel button - unload form
    '
    OnCancel
End Sub
Private Sub OnCancel()
    Hide
    cancelled = True
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        Cancel = True
        OnCancel
    End If
End Sub
Private Sub cmdGetFileOK_Click()
    '
    Dim r As Integer
    Dim wksNewSheet As Excel.Worksheet
    Dim TempVal As String, ErrorString As String
    '
    ' Check that all mandatory values have been completed
    '
    With Me
        If Len(Label5.Caption) = 0 Or Len(ComboBox1.Value) = 0 Then
            ErrorString = ErrorString + "Please complete all mandatory fields (marked by *)." & vbNewLine
            MsgBox ErrorString, vbCritical, "Values Missing"
            OnCancel
            Exit Sub
'        Else
'            cancelled = False
        End If
    End With
    If Not cancelled Then
        '
        ' Do some stuff
        '
    End If
End Sub

Any thoughts gratefully appreciated. I've been looking at this for hours.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Have you set a breakpoint in the cmdGetFileOK_Click routine and stepped through to see what is happening? If so, which lines get executed?
 
Upvote 0
This is how I do the close even so you could adapt it for yours
VBA Code:
Private Sub CButton_Close_Click()
    If EditForm.CButton_Save.Enabled = True Then
        If MsgBox("This will lose any unsaved data. Are you sure?", vbYesNo) = vbNo Then Exit Sub
    End If
    Reset_Form
' Hide but do not unload form
    EditForm.Hide

End Sub
 
Upvote 0
Have you set a breakpoint in the cmdGetFileOK_Click routine and stepped through to see what is happening? If so, which lines get executed?
It steps all the way to the End Sub and stops. In the VBA window the run button is greyed out and in the workbook the form is still displayed.
If I click 'Break' at that point the next line to execute is .Show.
 

Attachments

  • Screenshot 2024-01-30 195149.jpg
    Screenshot 2024-01-30 195149.jpg
    108.8 KB · Views: 9
  • Screenshot 2024-01-30 195950.jpg
    Screenshot 2024-01-30 195950.jpg
    32 KB · Views: 7
Last edited:
Upvote 0
If it doesn't process that If block, there is no code to hide the form.
 
Upvote 0
Solution
Glad you solved it. :)
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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