ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code help needed for Option Buttons to control Subs (https://www.excelbanter.com/excel-programming/439520-code-help-needed-option-buttons-control-subs.html)

Brian

Code help needed for Option Buttons to control Subs
 
I have a User form with (20) option buttons on it. It does what I want by
only being able to check 1 option button.

Option Button Names:
OB_601
OB_602 - OB_619
OB_620

What I need help with is when user checks the correct option button, that
button exicutes code located in a module.

Code in Module: There are (20) Subs named like below
Sub String_01()
Sub String_02() - Sub String_19()
Sub String_20()

I think the code for each Option button is something like this, because
there default position is False and they only show true when selected.

If OB_601 = True
then call Sub String_01()
If OB_602 = True
then call Sub String_02()
If OB_603 = True
then call Sub String_03()

Etc....
End Sub

OssieMac

Code help needed for Option Buttons to control Subs
 
Hi Brian,

While in Design mode if you right click on the option button and select View
Code it will take you to the VBA editor for the forms and create a Sub / End
sub. I would put the code in there like the following instead of in another
module .

Note that If Me.OB_601 is the same as
If Me.OB_601 = True

Private Sub OB_601_Click()
If Me.OB_601 Then
'Your code here
End If
End Sub

Private Sub OB_602_Click()
If Me.OB_602 Then
'Your code here
End If
End Sub

Private Sub OB_603_Click()
If Me.OB_603 Then
'Your code here
End If
End Sub

However, you can call the code in a standard module if that is what you want
to do but you will still need a sub for each of the option buttons. Example
as follows.

Private Sub OB_601_Click()
If Me.OB_601 Then
Call String_01
End If
End Sub

You normally only put code in a separate module if the same code is being
used for all the options with only minor changes dependant on the particular
button. You would call the sub and pass the option button as a parameter.
Without seeing all of your code it is hard to advise if this is the better
way.


--
Regards,

OssieMac


"Brian" wrote:

I have a User form with (20) option buttons on it. It does what I want by
only being able to check 1 option button.

Option Button Names:
OB_601
OB_602 - OB_619
OB_620

What I need help with is when user checks the correct option button, that
button exicutes code located in a module.

Code in Module: There are (20) Subs named like below
Sub String_01()
Sub String_02() - Sub String_19()
Sub String_20()

I think the code for each Option button is something like this, because
there default position is False and they only show true when selected.

If OB_601 = True
then call Sub String_01()
If OB_602 = True
then call Sub String_02()
If OB_603 = True
then call Sub String_03()

Etc....
End Sub


Brian

Code help needed for Option Buttons to control Subs
 
Would this code work or do I need to have 20 seperate subs like the one you
showed me?

Private Sub OB_601-620_Click()
If Me.OB_601 Then
Call String_01
End If
If Me.OB_602 Then
Call String_02
End If
If Me.OB_603 Then
Call String_03
End If
If Me.OB_604 Then
Call String_04
End If
End Sub

Thanks for your help

"OssieMac" wrote:

Hi Brian,

While in Design mode if you right click on the option button and select View
Code it will take you to the VBA editor for the forms and create a Sub / End
sub. I would put the code in there like the following instead of in another
module .

Note that If Me.OB_601 is the same as
If Me.OB_601 = True

Private Sub OB_601_Click()
If Me.OB_601 Then
'Your code here
End If
End Sub

Private Sub OB_602_Click()
If Me.OB_602 Then
'Your code here
End If
End Sub

Private Sub OB_603_Click()
If Me.OB_603 Then
'Your code here
End If
End Sub

However, you can call the code in a standard module if that is what you want
to do but you will still need a sub for each of the option buttons. Example
as follows.

Private Sub OB_601_Click()
If Me.OB_601 Then
Call String_01
End If
End Sub

You normally only put code in a separate module if the same code is being
used for all the options with only minor changes dependant on the particular
button. You would call the sub and pass the option button as a parameter.
Without seeing all of your code it is hard to advise if this is the better
way.


--
Regards,

OssieMac


"Brian" wrote:

I have a User form with (20) option buttons on it. It does what I want by
only being able to check 1 option button.

Option Button Names:
OB_601
OB_602 - OB_619
OB_620

What I need help with is when user checks the correct option button, that
button exicutes code located in a module.

Code in Module: There are (20) Subs named like below
Sub String_01()
Sub String_02() - Sub String_19()
Sub String_20()

I think the code for each Option button is something like this, because
there default position is False and they only show true when selected.

If OB_601 = True
then call Sub String_01()
If OB_602 = True
then call Sub String_02()
If OB_603 = True
then call Sub String_03()

Etc....
End Sub


OssieMac

Code help needed for Option Buttons to control Subs
 
Hi Brian,

I don't know of any way of grouping the code into one sub. You will need
separate subs for each button.

I have often thought that there should be an event to detect a change in the
group status and a Target parameter to identify the button that is True.

--
Regards,

OssieMac


"Brian" wrote:

Would this code work or do I need to have 20 seperate subs like the one you
showed me?

Private Sub OB_601-620_Click()
If Me.OB_601 Then
Call String_01
End If
If Me.OB_602 Then
Call String_02
End If
If Me.OB_603 Then
Call String_03
End If
If Me.OB_604 Then
Call String_04
End If
End Sub

Thanks for your help

"OssieMac" wrote:

Hi Brian,

While in Design mode if you right click on the option button and select View
Code it will take you to the VBA editor for the forms and create a Sub / End
sub. I would put the code in there like the following instead of in another
module .

Note that If Me.OB_601 is the same as
If Me.OB_601 = True

Private Sub OB_601_Click()
If Me.OB_601 Then
'Your code here
End If
End Sub

Private Sub OB_602_Click()
If Me.OB_602 Then
'Your code here
End If
End Sub

Private Sub OB_603_Click()
If Me.OB_603 Then
'Your code here
End If
End Sub

However, you can call the code in a standard module if that is what you want
to do but you will still need a sub for each of the option buttons. Example
as follows.

Private Sub OB_601_Click()
If Me.OB_601 Then
Call String_01
End If
End Sub

You normally only put code in a separate module if the same code is being
used for all the options with only minor changes dependant on the particular
button. You would call the sub and pass the option button as a parameter.
Without seeing all of your code it is hard to advise if this is the better
way.


--
Regards,

OssieMac


"Brian" wrote:

I have a User form with (20) option buttons on it. It does what I want by
only being able to check 1 option button.

Option Button Names:
OB_601
OB_602 - OB_619
OB_620

What I need help with is when user checks the correct option button, that
button exicutes code located in a module.

Code in Module: There are (20) Subs named like below
Sub String_01()
Sub String_02() - Sub String_19()
Sub String_20()

I think the code for each Option button is something like this, because
there default position is False and they only show true when selected.

If OB_601 = True
then call Sub String_01()
If OB_602 = True
then call Sub String_02()
If OB_603 = True
then call Sub String_03()

Etc....
End Sub


OssieMac

Code help needed for Option Buttons to control Subs
 
Hi again Brian,

Not certain that I understand correctly. Is Update_Installer_Forms_10 a
command button?

If so and that is the way you want to go by making the selection then
clicking a command button then you can use a loop and Run command and
concatenate the sub names to be called.

For i = 601 To 620
On Error GoTo NoSelection
If UserForm1("Battery_String_Qty_" & i) Then
On Error GoTo 0 'Reset error trapping
Run "Battery_String_" & i
Exit For
End If
Next i

Exit Sub

NoSelection:
MsgBox "No buttons selected." & vbLf _
& "Processing terminated."
Exit Sub

However, the above code uses sub names Battery_String_601 to
Battery_String_620. That is the method I would go with and keep the numeric
part of the names the same but you could change to the following and just use
the last 2 digits like the following.

Dim i As Long

For i = 601 To 620
On Error GoTo NoSelection
If UserForm1("Battery_String_Qty_" & i) Then
On Error GoTo 0 'Reset error trapping
Run "Battery_String_" & Mid(i, 2)
Exit For
End If
Next i

Exit Sub

NoSelection:
MsgBox "No buttons selected." & vbLf _
& "Processing terminated."
Exit Sub

--
Regards,

OssieMac




All times are GMT +1. The time now is 03:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com