Excel 用户表单和复选框值

Excel Userform sum Checkbox Values

目前我正在用户表单中的“记分卡”中工作,用户将在其中看到一个选项列表和一个彼此相邻的复选框。如果复选框为“假”,则该值将为 0,或者如果复选框为“真”,则该值可以从 -3 到 3。

我试过用这样的东西:

Private Sub checkbox1_Click()
If checkbox1 = True Then
checkbox1 = 1
Else
checkbox1 = 0 
End Sub
'**********************************
Private Sub checkbox2_Click()
If checkbox2 = True Then
checkbox2 = -1
Else
checkbox2 = 0 
End Sub
'**********************************
Private Sub checkbox3_Click()
If checkbox3 = True Then
checkbox3 = 1
Else
checkbox3 = 0 
End Sub

当用户 select 复选框时,它将对所有值求和并在用户表单中自动生成的标签中提供最终结果

Private Sub UserForm_Click()
valsum = Sum checkbox1, checkbox2, checkbox3 ' sums all the checkboxes
if valsum > 0 then
label1.caption = "Good to Go"
elseif valsum = 0 then
label1.caption = "Caution"
else 'if < 0
label1.caption = "You are Bad"
end Sub

我想避免在工作表中包含数据,而只包含所有内容 运行“后端” 编辑:这是它的外观。该值应该预先定义,例如,如果 checkbox1 为真,则 cb1= 3,否则 cb1=0。如果 checkbox2 为真,则 cb2=1.....然后将这些值加在一起

您似乎在尝试为 CheckBoxes 分配一个数值。我看对了吗?您几乎只能将 TrueFalse 值分配给 CheckBox(您也可以将 tri-state 的值设为 Null 值)。布尔值确实是 1 或 0,但你不能为它们分配 +1、0 或 -1。

我认为您需要有 3 个复选框,然后为每个包含数值的复选框(例如文本框或组合框)关联一个控件。然后,您可能会根据是否勾选了 CheckBox 来启用或禁用那些关联的控件。

假设用户窗体如下所示:

您的框架代码可能是这样的:

Option Explicit

Private Sub CheckBox1_Change()
    EnableTextBox TextBox1, CheckBox1
End Sub

Private Sub CheckBox2_Change()
    EnableTextBox TextBox2, CheckBox2
End Sub

Private Sub CheckBox3_Change()
    EnableTextBox TextBox3, CheckBox3
End Sub

Private Sub EnableTextBox(tbox As MSForms.TextBox, chbox As MSForms.CheckBox)
    If chbox.Value Then
        With tbox
            .Enabled = True
            .Text = ""
        End With
    Else
        With tbox
            .Enabled = False
            .Text = "n/a"
        End With
    End If
End Sub

Private Sub CommandButton1_Click()
    Dim tot As Double
    
    tot = TextToDouble(TextBox1.Text) + TextToDouble(TextBox2.Text) + TextToDouble(TextBox3.Text)
    Label1.Caption = tot
End Sub

Private Function TextToDouble(txt As String) As Double
    On Error Resume Next
    TextToDouble = CDbl(txt)
End Function

像下面这样的东西,我不知道你是怎么填的,但基本上这样应该可以。 Combobox1_change 等在 combobox 值更改时启动事件,然后每个事件甚至检查值是否为数字并调用一个方法来对值求和并将其分配给 label

Private Sub UserForm_Initialize()
For i = 1 To 10
    ComboBox1.AddItem (i)
    ComboBox2.AddItem (i)
    ComboBox3.AddItem (i)
Next i
End Sub
Private Sub ComboBox1_Change()
'call sum and display method
Call Calc
End Sub
Private Sub ComboBox2_Change()
'call sum and display method
Call Calc
End Sub
Private Sub ComboBox3_Change()
'call sum and display method
Call Calc
End Sub
Private Sub Calc()
'method to update
Dim total As Double
'check if value of combobox is numeric
If IsNumeric(ComboBox1.value) Then
  total = total + ComboBox1.value
End If
If IsNumeric(ComboBox2.value) Then
    total = total + ComboBox2.value
End If
If IsNumeric(ComboBox3.value) Then
    total = total + ComboBox3.value
End If
Label1 = total
End Sub

编辑 好吧,我只是注意到我很累,我完全误解了这个问题,抱歉 :( 现在,我不记得我从哪里得到 Combobox 值...

您似乎需要以下内容:

Option Explicit

Private valsum As Integer

Private Sub checkbox1_Click()
   SumAllValues
End Sub

Private Sub checkbox2_Click()
   SumAllValues
End Sub

Private Sub checkbox3_Click()
   SumAllValues
End Sub

Private Sub checkbox4_Click()
   SumAllValues
End Sub

Private Sub SumAllValues()
   valsum = 0
   valsum = valsum + IIf(CheckBox1.Value = True, 3, 0)
   valsum = valsum + IIf(CheckBox2.Value = True, 1, 0)
   valsum = valsum + IIf(CheckBox3.Value = True, -2, 0)
   valsum = valsum + IIf(CheckBox4.Value = True, 2, 0)
   
   If valsum > 0 Then
      Label1.Caption = "Good to Go"
   ElseIf valsum = 0 Then
      Label1.Caption = "Caution"
   Else 'if < 0
      Label1.Caption = "You are Bad"
   End If
End Sub