Grouped option buttons on forms

J

johnhildreth

This should be easy...how does one determine which of a set of grouped
option buttons is selected? Let's say I have 4 option buttons
(optionbutton1, optionbutton2, etc) with groupname set to Season. The
user selects....winter, which corresponds to optionbutton4. I can
check each one...

If Userform1.OptionButton1.Value = -1 Then x = 1
If Userform1.OptionButton2.Value = -1 Then x = 2
If Userform1.OptionButton3.Value = -1 Then x = 3
If Userform1.OptionButton4.Value = -1 Then x = 4

but there MUST be a better way!! I have many groups and many, many
option buttons on my form. The above solution quickly becomes
tedious.

Thanks,
John
 
M

merjet

Dim c As Control
Dim x As Integer
For Each c In UserForm1.Controls
If c.GroupName = "Season" And c.Value = True Then
x = Right(c.Name, Len(c.Name) - 12)
End If
Next c

Hth,
Merjet
 
T

Tom Ogilvy

for i = 1 to 4
set ctrl = me.controls("OptionButton" & i)
if ctrl.Value then
x = i
exit for
end if
next

There is no me.Groups(groupname).Value that will tell you the name of the
optionbutton or anything like that.
 
J

johnhildreth

Dim c As Control
Dim x As Integer
For Each c In UserForm1.Controls
If c.GroupName = "Season" And c.Value = True Then
x = Right(c.Name, Len(c.Name) - 12)
End If
Next c

Hth,
Merjet



I spoke too soon....I want to check each by GroupName and Value, but
c.GroupName is not valid. Only OptionButtons have GroupNames,
controls do not. So I tried:

Dim o As OptionButton

DrngPied.Show 'DrngPied is the form

For Each o In DrngPied.Controls
If o.GroupName = "SizeOptions" And o.Value = -1 Then
sSize = c.Name
End If
Next o


but I get a Type Mismatch at the For Each line.

John
 
D

Dave Peterson

Maybe...

Dim o As Control
drngPied.Show
For Each o In drngPied.Controls
If TypeOf o Is MSForms.OptionButton Then
If o.GroupName = "SizeOptions" And o.Value = -1 Then
sSize = c.Name
End If
End If
Next o
 

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