简体   繁体   中英

Fill a textbox with varaible text depending on Radio Button selection combo (VBA Excel)

I need some help with getting the right code to do the following:

  • I have 4 groups of radio buttons inside a frame in a userform
  • Each group is a simple Yes/No radio button
  • I have a textbox that I want to autofill with a score range of AD depending on the # of "yes" radio buttons selected.
  • The "No" checkboxes really shouldn't do anything in regards to the textbox

    • Userform Name = TP_UF
    • Frame Name = fun_opt_frame
    • Option Button Name for "Yes" = fun_score_yes1-4
    • Textbox Name = fun_scorebox

Logic:

  • 4 Yesses = A
  • 3 Yesses = B
  • 2 Yesses = C
  • 1 Yes = D

It doesn't matter what order the yesses are selected, its a total count. I tried using code using the frame but not sure if that is the best way. The frame for these radio buttons isn't needed for any reason other then to perhaps make it easier to code. So I could throw out the frame if it's not necessary to get this working.

I am not sure where to start here. Any help would be appreciated.

pic

The quickest and easiest way for you to understand is - I guess - the following code. You have to put the code into the class module of the userform.

Option Explicit

Dim opt1 As Byte
Dim opt2 As Byte
Dim opt3 As Byte
Dim opt4 As Byte

Private Sub opt1Yes_Click()
    opt1 = 1
    EvalOpt
End Sub
Private Sub opt1No_Click()
    opt1 = 0
    EvalOpt
End Sub
Private Sub opt2yes_Click()
    opt2 = 1
    EvalOpt
End Sub
Private Sub opt2No_Click()
    opt2 = 0
    EvalOpt
End Sub
Private Sub opt3yes_Click()
    opt3 = 1
    EvalOpt
End Sub
Private Sub opt3No_Click()
    opt3 = 0
    EvalOpt
End Sub
Private Sub opt4yes_Click()
    opt4 = 1
    EvalOpt
End Sub
Private Sub opt4No_Click()
    opt4 = 0
    EvalOpt
End Sub

Private Sub EvalOpt()
Dim sumOpt As Byte
Dim res As String
    sumOpt = opt1 + opt2 + opt3 + opt4
    Select Case sumOpt
    Case 1: res = "D"
    Case 2: res = "C"
    Case 3: res = "B"
    Case 4: res = "A"
    Case Else: res = ""
    End Select
    Me.fun_scorebox.text = res
End Sub

I assumed the option buttons are named opt1Yes, opt1No, opt2Yes, opt2No etc. A more advanced solution would probably be to use classe modules and "collect" the option buttons in such a way.

I ended up going about this differently and I got it working using a counter. Thanks for the help! Posting code here in case anyone else needs it.

    Option Explicit

    Private Sub OptionButton1_Change()
    set_counter
    End Sub

    Private Sub OptionButton2_Change()
    set_counter
    End Sub

    Private Sub OptionButton3_Change()
    set_counter
    End Sub

    Private Sub OptionButton4_Change()
    set_counter
    End Sub

    Private Sub OptionButton5_Change()
    set_counter
    End Sub

   Private Sub OptionButton6_Change()
   set_counter
   End Sub

   Private Sub OptionButton7_Change()
   set_counter
   End Sub

   Private Sub OptionButton8_Change()
   set_counter
   End Sub

   Private Sub set_counter()
    Dim x As Integer, counter As Integer
     Me.TextBox1.Value = ""
     counter = 0
     For x = 1 To 8 Step 2
       If Me.Controls("OptionButton" & x).Value = True Then counter = counter + 1
     Next x
        Me.TextBox1.Value = Choose(counter, "D", "C", "B", "A")
   End Sub

  Private Sub UserForm_Activate()
    Me.TextBox1.Value = ""
  End Sub

  Private Sub UserForm_Click()
    Dim x As Integer
      Me.TextBox1.Value = ""
      For x = 1 To 8
         Me.Controls("OptionButton" & x).Value = False
      Next x
  End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM