根据单选按钮选择组合 (VBA Excel) 用可变文本填充文本框

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

我需要一些帮助来获得正确的代码来执行以下操作:

逻辑:

选择是什么顺序并不重要,它是一个总数。我尝试使用框架使用代码,但不确定这是否是最好的方法。除了可能使编码更容易之外,出于任何原因都不需要这些单选按钮的框架。所以如果没有必要让这个工作,我可以扔掉框架。

我不知道从哪里开始。任何帮助,将不胜感激。

pic

您理解的最快和最简单的方法是 - 我猜 - 以下代码。您必须将代码放入用户表单的 class 模块中。

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

我假定选项按钮被命名为 opt1Yes、opt1No、opt2Yes、opt2No 等。 更高级的解决方案可能是以这种方式使用 classe 模块和 "collect" 选项按钮。

我最终以不同的方式解决了这个问题,我使用计数器让它工作。谢谢您的帮助!在这里发布代码以防其他人需要它。

    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