UserForm does not close

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,263
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?
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,957
Please post your workbook, with the code, to a CLOUD site for download so it can be inspected first hand.
 

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,263
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,359
Messages
5,624,238
Members
416,018
Latest member
mirceaon

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
Top