PDA

View Full Version : Turning Multiple Optionbuttons subs into one sub



Ladyj205
05-01-2018, 09:44 AM
I have MS Office 2010

I have 4 optionbuttons and each has there own value such as:

optionbutton 3 is 75
optionbutton 2 is 50
optionbutton 1 is 25
optionbutton 0 is 0

is there a vba for having one sub instead of having multiple sub.

here is my code that i have now:

Option Explicit
Private Sub OptionButton0_Click()
If OptionButton0 Then
Range("D44").Value = 0
End If
End Sub
Private Sub OptionButton1_Click()
If OptionButton1 Then
Range("D44").Value = 25
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2 Then
Range("D44").Value = 50
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3 Then
Range("D44").Value = 75
End If
End Sub

i want to go for something like this
Sub CalcDisplay()
Dim arrParts() As Variant
Select Case True
Case Opt_1_3: .Text = "75"
Case Opt_1_2: .Text = "50"
Case Opt_1_1: .Text = "25"
Case Opt_1_0: .Text = "0"
End Select


any examples and help would be greatly appreciated. : pray2: Thanks

MickG
05-02-2018, 02:56 AM
Maybe This:-


Private Sub OptionButton1_Click()
Opt OptionButton1
End Sub


Private Sub OptionButton2_Click()
Opt OptionButton2
End Sub



Private Sub OptionButton3_Click()
Opt OptionButton3
End Sub



Private Sub OptionButton4_Click()
Opt OptionButton4
End Sub



'NB:- Place the code below in basic module:-
Sub Opt(oP As Object)
Select Case True
Case oP.Name = "OptionButton1": Range("D44").Value = 0
Case oP.Name = "OptionButton2": Range("D44").Value = 25
Case oP.Name = "OptionButton3": Range("D44").Value = 50
Case oP.Name = "OptionButton4": Range("D44").Value = 75
End Select
End Sub

SamT
05-02-2018, 07:35 AM
s there a vba for having one sub instead of having multiple sub.I don't think you can do that with controls on a worksheet. :dunno:

You can with a UserForm, but it requires a "With Events" Module.