OPtion Button

A

art

Hello:

I made a userform with 2 option buttons, however When I reference the option
buttons in another vba module, it does not recognize it? It always diplays it
either as 0 value or false. How can reference the option button in another
code?

Please help.
 
J

JLGWhiz

UserForms are not part of a module, they exist at the project level. To
refer to a control on the UserForm, such as an option button, from outside
the UserForm code, you would have to include the UserForm in the reference:

myVar = UserForm1.OptionButton1.Value
 
C

Chip Pearson

And the UserForm must be active when calling for any control values.

Nope, not true. For example,

Load UserForm1
UserForm1.TextBox1.Text = "abcd"
Debug.Print UserForm1.TextBox1.Text
Unload UserForm1

As long as the form is still in memory (hidden with Hide but not
dumped with Unload), you can access control values.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
A

art

I don't understand what you are saying.
What I did was just hide the userform instead of unloading it, so the users
selection still stays. Is there another way to do that?

Thanks for responding.
 
J

JLGWhiz

The option button has two values, True and False. As Chip Pearson pointed
out, you should be able to access the option button value if the form is
hidden, but not if you unload it. Look up Unload in the VBA help file.
Access VBA help by pressing Alt + F11.

If your UserForm is UserForm1 and your OptionButton is OptionButton1, then
copy and paste the code below into your code module1.

Sub optBtnTest()
Load UserForm1
MsgBox UserForm1.OptionButton1.Value
Unload UserForm1
End Sub

Then run the macro.

You can also set the Control Source in the OptionButton properties to a cell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top