Preventing A Userform From Closing

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,590
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Please consider this code,

Rich (BB code):
Private Sub create1_Click()
     
    Dim strFilename As String
    Dim ws_vh As Worksheet
    Dim no_rw As Long
    Dim ui1 As VbMsgBoxResult
    Dim oFS As Object
    Dim uimsg As Long
    Dim fw As Long
    
    Application.ScreenUpdating = False
    strFilename = "H:\PWS\Parks\Parks Operations\Sports\Sports15\CLASS_DUMP\schedule.csv"
    Set oFS = CreateObject("Scripting.FileSystemObject")
    
    Set ws_vh = Workbooks("Sports17.xlsm").Worksheets("VAR_HOLD")
    
    f_name = "schedule.csv"
    If CheckFileIsOpen(f_name) = True Then
        Debug.Print "schedule.csv is open and hidden"
        Workbooks(f_name).Close False
    End If
    Debug.Print "schedule.csv is not open. It will be opened now."
    Workbooks.Open Filename:=strFilename                                                            'open schedule.csv
    Workbooks("schedule.csv").Windows(1).Visible = False                                            'hide it
    
    With Workbooks("schedule.csv").Worksheets("schedule")
        no_rw = WorksheetFunction.CountIf(.Range("M:M"), ws_vh.Range("B17"))                         'determine number of rows of raw data that match the selected dates
        ws_vh.Range("F17") = no_rw
    End With
    Application.ScreenUpdating = True
    
    'If rows of data are found for the selected date
    
    If no_rw > 0 Then
        msg_raw_data_in_file.Show
    Else
        msg_data_not_in_schedule.Show
    End If
End Sub

In our example, lets assume no_rw = 21. If no_rw > 0 then open the userform called 'msg_raw_data_in_file'.
The userform opens, and has a command button called "CANCEL". If the user selects this option, he is returned to a default state of the application, which consists of activating the home worksheet and opening the preliminary application's userform (uf1_create-wo1)

Here is the cancel commandbutton code"

Rich (BB code):
Private Sub CommandButton3_Click() 'CANCEL
    Workbooks("schedule.csv").Close savechanges:=False
    Application.ScreenUpdating = True
    Unload msg_raw_data_in_file
    Unload uf1_create_wo1
    uf1_create_wo1.Show
    Workbooks("sports17.xlsm").Worksheets("FRONT").Activate
End Sub

Here is my problem ...
When the above code is executed, the home worksheet is activated, and the userform initiatizes and becomes visible. But after that, the code returns to procedure 'create1_click' at the line highlighted in blue. The line in red unfortunately closes uf1_create_wo1, which I don't want.

What can I do to prevent the userform from closing?
 
I've tried :
Code:
Private Sub CommandButton5_Click() 'OK
    Me.Hide
End Sub

And it seems to be part of the answer. msg_crp_reqd is out of site (hidden) and uf1_create_wo1 becomes the focus and is accessible. However, not sure how to close msg_crp_reqd. I have an exit button on uf1_create_wo1 but it can be used regardless of the state that msg_crp_reqd is. I need the Exit button to close any hidden forms in addition to uf1_create_wo1.
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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