Search code examples
vbadialoguserformpowerpoint

VBA multiple option dialog box output


I created a user form with multiple options and now I want that the option the user selects is shown in a label under the button that calls the user form.I changed the caption in the text box under the button to resemble what should happen

However my options aren't working. Should I save the output in a global variable and then call it back to change the label and if so how do I do that? Or is it possible to just call the selection within the user form?

The code I was trying to run was this one to call the message box and then change the text box which is actually a label called "labelpage"

Private Sub CommandButton1_Click()

UserForm1.Show
If UserForm1.OptionButton1 = True Then LabelPage.Caption = "Company Restricted"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Strictly Confidential"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Public Information (does not need to be marked)"

End Sub

I also had this for each button click just to close them after selection, within the user form code.

Private Sub OptionButton1_Click()
    OptionButton1.Value = True
    Unload Me
End Sub

Private Sub OptionButton2_Click()
    OptionButton2.Value = True
    Unload Me
End Sub

Private Sub OptionButton3_Click()
    OptionButton3.Value = True
    Unload Me
End Sub

Is there just a tiny mistake of syntax or something like that or is this just completely wrong? Thank you in advance for your help.


Solution

  • The issue is that you are unloading the UserForm, meaning the controls are not available to you. The solution is to just hide the UserForm:

    Private Sub OptionButton1_Click()
        Hide
    End Sub
    
    Private Sub OptionButton2_Click()
        Hide
    End Sub
    
    Private Sub OptionButton3_Click()
        Hide
    End Sub