Consulting

Results 1 to 3 of 3

Thread: Turning Multiple Optionbuttons subs into one sub

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    50
    Location

    Turning Multiple Optionbuttons subs into one sub

    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. Thanks

  2. #2
    VBAX Regular
    Joined
    Jan 2011
    Posts
    35
    Location
    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

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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. :

    You can with a UserForm, but it requires a "With Events" Module.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •