UserForm does not close

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code in the Workbook Open event.

Code:
    Application.Visible = False
    UserForm.show

When UserForm opens, some code opens another userform called 'frmMessageBox2' which contains two buttons and a label. The two buttons have their captions called Yes and No respectively. The code that runs when these buttons are pressed is:-

Code:
Private Sub cmdNo_Click()

    strYN = "No"
    Unload Me

End Sub

Private Sub cmdYes_Click()

    strYN = "Yes"
    Unload Me
    
End Sub

The code on the Initialize event of UserForm is:-

Code:
    If DoesRegistryKeyExist("HKEY_CURRENT_USER\Software\Microsoft\Office\" & dblMSOfficeVersion & "\Excel\Security\Trusted Locations\Licence Key Generator\") = False Then
        With frmMessageBox2
            .lblMessage.Caption = strMessage
            .show
        End With
        
        If strYN = "No" Then
            Exit Sub
        ElseIf strYN = "Yes" Then
           'some code
        End If
    End If

When the Yes button is pressed then everything works fine and 'frmMessageBox2' closes. However when the No button is pressed, frmMessageBox2 appears again each time I presse the No button. What am I doing wrong?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Please post your workbook, with the code, to a CLOUD site for download so it can be inspected first hand.
 
Upvote 0
Please post your workbook, with the code, to a CLOUD site for download so it can be inspected first hand.
Solved the problem. I had the following lines before opening 'frmMessageBox2' form which in effect was reactivating the form.

Code:
        Me.lstLicences.Visible = True
        Me.lstLicences.SetFocus

Don't understand why this would happen, but it seems to have solved the issue. I have placed the opening of 'frmMessageBox2' at the beginning of the code so that any subsequent code can work without re-activatibg and going into a loop. Hope that makes sense.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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