Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default OptionButton type name?

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default OptionButton type name?

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]


Charley Kyd wrote:
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default OptionButton type name?

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" wrote in message
...
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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default OptionButton type name?

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



"Charley Kyd" wrote in message
...
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 120
Default OptionButton type name?

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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
optionbutton - three options Sian Excel Discussion (Misc queries) 7 January 5th 08 10:29 AM
Optionbutton troubles Mats Samson Excel Worksheet Functions 2 September 27th 07 03:54 PM
OptionButton. Reset to 0 Eef Houniet New Users to Excel 7 September 3rd 06 12:14 PM
How Do I Tab From An OptionButton To A TextBox Minitman Excel Discussion (Misc queries) 0 February 23rd 05 09:34 PM
Use Arrow keys in optionbutton JON-JON Excel Programming 3 September 3rd 03 08:03 AM


All times are GMT +1. The time now is 01:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"