OptionButton type name?

C

Charley Kyd

I'm having a TypeName problem and need some ideas.

To see my problem, put two option buttons from Control Toolbox (not Forms)
on your worksheet. Then, in the Sheet1 module, set up this code:

Private Sub OptionButton1_Click()
Foo OptionButton1
End Sub

Private Sub OptionButton2_Click()
Foo OptionButton2
End Sub

Sub Foo(Button As OptionButton)
Debug.Print "Success!"
End Sub

When I click on an option button I get a Run Time Error 13, Type mismatch.

This is strange, because when I use the Immediate pane to enter...
Print TypeName(OptionButton1)
....I'm told that OptionButton12 is, indeed, an OptionButton.

Obviously, in Foo, I could define Button as a variant. But that just ignores
the problem. Why am I getting an error when I define an option button as an
option button?

Thanks.

Charley
 
J

Jake Marx

Hi Charley,

Preface the OptionButton in your subroutine argument with "MSForms" and it
will work:

Sub Foo(Button As MSForms.OptionButton)

And you should probably name your argument something else - maybe opt -- as
"Button" is a reserved keyword.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
C

Chip Pearson

Charley,

Because both MSForms and Excel have objects named "OptionButton",
you need to indicate which option button you are using. If you
don't, the compiler will use the OptionButton object defined in
the Excel library (which is the one on the Forms toolbar) rather
than the one defined in the MSForms library (which is the one on
the Controls toolbar).

Change the declaration in your Foo procedure to include the type
library name of the option button. For example,

Sub Foo(Button As MSForms.OptionButton)
Debug.Print "Success!"
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





Charley Kyd said:
I'm having a TypeName problem and need some ideas.

To see my problem, put two option buttons from Control Toolbox (not Forms)
 
C

Charley Kyd

Thanks, Jake and Chip. I thought it must be something like that.

And with regard to naming variables, I'm a strong proponent of hungarian
notation. So, in the actual code, "Button" probably will be changed to
"btnCurrent".

Charley
 
W

Wei-Dong Xu [MSFT]

Hi Charley,

Thank you for replying!

The suggestion and analysis from Jade and Chip are very great! The cause for this error is the missing of MSFORM for the optionaButton.

Please feel free to let me know if you have any further questions.

Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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