PC Review


Reply
Thread Tools Rate Thread

Detect Option Group Value in VBA

 
 
Steve
Guest
Posts: n/a
 
      12th Apr 2009
I have a set of three option buttons (from the control toolbox) called
"MyGroup" that I would like to detect which of the three is true in VBA and I
cannot figure out how to refer to the group in VBA. Is there a way to detect
the value of the group or do I have to cycle through each option button
individually using if statements or select case statements?

Thanks for your help.

Steve
 
Reply With Quote
 
 
 
 
Harald Staff
Guest
Posts: n/a
 
      12th Apr 2009
Hi Steve

The optionbuttons, not the group, contains the value. So loop, like:

If optionbutton1.value = True then
'xxx
elseif optionbutton2.value = true then
'yyy
elseif 'and so on


I think it's better is to assign the result at the time a button is clicked,
like:

Dim GroupValue1 As Long

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then GroupValue1 = 1
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then GroupValue1 = 2
End Sub

HTH. Best wishes Harald

"Steve" <(E-Mail Removed)> wrote in message
news:E2AC8B57-FD02-4B32-BE4A-(E-Mail Removed)...
>I have a set of three option buttons (from the control toolbox) called
> "MyGroup" that I would like to detect which of the three is true in VBA
> and I
> cannot figure out how to refer to the group in VBA. Is there a way to
> detect
> the value of the group or do I have to cycle through each option button
> individually using if statements or select case statements?
>
> Thanks for your help.
>
> Steve


 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      12th Apr 2009

Try a function like the following:

Function SelectedButtonFromGroup(WS As Worksheet, _
GroupName As String) As MSForms.OptionButton

Dim OleObj As OLEObject
Dim OPT As MSForms.OptionButton

For Each OleObj In WS.OLEObjects
If TypeOf OleObj.Object Is MSForms.OptionButton Then
Set OPT = OleObj.Object
If StrComp(OPT.GroupName, GroupName, vbTextCompare) = 0 Then
If OPT.Value <> 0 Then
Set SelectedButtonFromGroup = OPT
Exit Function
End If
End If
End If
Next OleObj
End Function


You pass in the Worksheet and GroupName and it will return the
OptionButton object that is selected in the specified group. You can
then call this function from your code with something like

Dim SelOpt As MSForms.OptionButton
Set SelOpt = SelectedButtonFromGroup( _
Worksheets("Sheet1"), "Group1")
If SelOpt Is Nothing Then
Debug.Print "none checked or GroupName is invalid."
Else
Debug.Print "Opt Button '" & SelOpt.Caption & "' is checked"
End If

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





On Sat, 11 Apr 2009 18:05:01 -0700, Steve
<(E-Mail Removed)> wrote:

>I have a set of three option buttons (from the control toolbox) called
>"MyGroup" that I would like to detect which of the three is true in VBA and I
>cannot figure out how to refer to the group in VBA. Is there a way to detect
>the value of the group or do I have to cycle through each option button
>individually using if statements or select case statements?
>
>Thanks for your help.
>
>Steve

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subform w/table records as option choices (NOT OPTION GROUP) Robin Microsoft Access Forms 7 14th Nov 2008 09:19 AM
need help on how to grey out one option button in one group box based on the selection of another option button in another group box George Microsoft Excel Programming 13 11th Mar 2007 02:08 PM
Binding Values from a Table to Option Buttons contained in an Option Group faerewing@gmail.com Microsoft Access 3 15th Jan 2007 10:01 PM
Scanning Option Group (VB 6 Option Button Control Array) Michael D. Ober Microsoft VB .NET 6 21st Jun 2006 06:55 AM
making option groups visible/invisible based on another option group selection Emma Microsoft Access Forms 3 24th Jun 2004 05:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:38 AM.