Okay so here is the problem: In my user form, I have 2 checkboxes. I want to be able to use the value of the checkbox to perform a certain job.
Example:
Sub main()
UserForm1.Show
If UserForm1.CheckBox1.Value=True Then
MsgBox("Awesome")
End If
End Sub
Now my problem with this is that it keeps giving me run time error 424. Can anyone help me with this? Your help is greatly appreciated. Thank you.
Update:
Sub main()
UserForm1.Show
If UserForm1.CheckBox1.Value=True Then
Worksheets(1).Activate
If UserForm1.CheckBox1.Value=True Then
MsgBox("Awesome")
End If
End If
End Sub
Okay now it stops after worksheets(1).Activate
.
Your help is greatly appreciated.
Thank you.
Solution: This code works for me:
Explanation: The error appears because you did not specify which object (in this case: which form)
CheckBox1
belongs to. Hence I added theUserForm1.
in theIf
statement. Secondly,CheckBox1.Value
is a boolean property, i.e. the value will beTrue
when checked, not1
.Additional information: Please note that running the
If
clause just afterUserForm1.Show
(like you did in your example) will never work in case you intend to select the checkboxes after the.Show
command. The form will be shown and theIf
clause be run before you even had the time to select the checkbox. So the code in my answer should go to another Sub, e.g. the one run when you click a button in your form (do you have some sort of "OK" or "Close" button on it? If yes, double click the button in the macro editor and add the code there). Let me know if you need more context.Update (as requested in the comments): Here's what I have:
Running
a
(from the "Macros" dialogue on the "Developer" tab) gives me:Selecting the checkbox and clicking OK returns this: