Determining which radio button is selected

dsimcox

Board Regular
Joined
Dec 8, 2004
Messages
75
I am using 10 radio buttons to determine which production line report to create.

I can use IF statements to accomplish this, but I'm wondering if it is possible to loop through the radio buttons, testing programmatically which one is "True"

My radio buttons are named opLine1 thru opLine10.
I've tried this with no success.

************************************************************
Dim thisBtn as OptionButton, LineNbr as Integer

For LineNbr = 1 To 10

Set thisBtn.name = "opLine" & LineNbr

If thisBtn.value = True Then

Call GetLineTotals (LineNbr) ' a sub to create a report

Exit Sub

End If

Next


Anyone have experience working with controls in this manner?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello there,

You didn't mention if your options are directly on a worksheet or if they are on a user form:

If they are on the worksheet use code as below:

Code:
Sub findoptionbuttonselected()
Dim obi As OLEObject
For Each obi In ActiveSheet.OLEObjects
If obi.ProgId = "Forms.OptionButton.1" And obi.Object.Value = True Then
MsgBox obi.Name
End If
Next
Set obi = Nothing
End Sub

Now if your options are on a form use code below:

Code:
Sub trouveOption()
Dim controli As Control
For Each controli In Me.Controls
If TypeOf controli Is msforms.OptionButton And controli.Value = True Then
MsgBox controli.Name
End If
Next
Set controli = Nothing
End Sub
 
Upvote 0
Your solution is perfect!

My radio buttons are on a userform.

I used your example to create a Function that I can use to determine and return which button is clicked.

I named the buttons opLine1, opLine2 . . . opLine10

Then I used string function to extract the numeric portion of the name, which I use in my code to address a particular production line.

Code:
Function FindBtnSelected(uf, BtnName)       '   supply name of userform (uf) and name of button, less numeric suffix (opLine)
Dim Btn As Control, thisBtn, FindBtnSelected as Integer

'     LOOP THRU ALL THE CONTROLS
For Each Btn In uf.Controls

'     LOOK FOR THOSE THAT ARE OPTION BUTTONS AND HAVE THE NAME "opLine" FOR A NAME
If TypeOf Btn Is MSForms.OptionButton And Left(Btn.Name, Len(BtnName)) = BtnName Then

'     TEST IF THE BUTTON IS SELECTED
    If Btn.value = True Then

'     ASSIGN A VARIABLE TO THAT BUTTON'S NAME 
        thisBtn = Btn.Name

'     IDENTIFY THE NUMERIC PORTION TO RETURN TO THE CALLING PROCEDURE
        FindBtnSelected = CInt(Mid(thisBtn, Len(BtnName) + 1, Len(thisBtn) - Len(BtnName)))

'     WHEN THE SELECTED BUTTON IS FOUND, EXIT THE FUNCTION
        Exit Function
    Else:
        GoTo tryNextBtn
    End If
End If

tryNextBtn:
Next

End Function

Many thanks for the nice assist, Francoise
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top