我创建了一些复选框,我在列表框中选择,如果我可以做什么 添加一个值来总结它们,我创建它作为餐厅菜单,我没有 知道如何总结,也许有人可以帮助我。 我是Visual Basic for Applications的初学者,我不知道如何做到这一点。也许我需要添加一个新的列表框,其中将显示值,然后将它们相加
`Private Sub CheckBox1_Change()
If CheckBox1.Value = True Then
ListBox1.AddItem CheckBox1.Caption
CheckBox1.Enabled = False
End If
End Sub
Private Sub CheckBox10_Change()
If CheckBox10.Value = True Then
ListBox1.AddItem CheckBox10.Caption
CheckBox10.Enabled = False
End If
End Sub
Private Sub CheckBox11_Change()
If CheckBox11.Value = True Then
ListBox1.AddItem CheckBox11.Caption
CheckBox11.Enabled = False
End If
End Sub
Private Sub CheckBox12_Change()
If CheckBox12.Value = True Then
ListBox1.AddItem CheckBox12.Caption
CheckBox12.Enabled = False
End If
End Sub
Private Sub CheckBox2_Change()
If CheckBox2.Value = True Then
ListBox1.AddItem CheckBox2.Caption
CheckBox2.Enabled = False
End If
End Sub
Private Sub CheckBox3_Change()
If CheckBox3.Value = True Then
ListBox1.AddItem CheckBox3.Caption
CheckBox3.Enabled = False
End If
End Sub
Private Sub CheckBox4_Change()
If CheckBox4.Value = True Then
ListBox1.AddItem CheckBox4.Caption
CheckBox4.Enabled = False
End If
End Sub
Private Sub CheckBox5_Change()
If CheckBox5.Value = True Then
ListBox1.AddItem CheckBox5.Caption
CheckBox5.Enabled = False
End If
End Sub
Private Sub CheckBox6_Change()
If CheckBox6.Value = True Then
ListBox1.AddItem CheckBox6.Caption
CheckBox6.Enabled = False
End If
End Sub
Private Sub CheckBox7_Change()
If CheckBox7.Value = True Then
ListBox1.AddItem CheckBox7.Caption
CheckBox7.Enabled = False
End If
End Sub
Private Sub CheckBox8_Change()
If CheckBox8.Value = True Then
ListBox1.AddItem CheckBox8.Caption
CheckBox8.Enabled = False
End If
End Sub
Private Sub CheckBox9_Change()
If CheckBox9.Value = True Then
ListBox1.AddItem CheckBox9.Caption
CheckBox9.Enabled = False
End If
End Sub
Private Sub CommandButton1_Click()
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
CheckBox6.Enabled = True
CheckBox7.Enabled = True
CheckBox8.Enabled = True
CheckBox9.Enabled = True
CheckBox10.Enabled = True
CheckBox11.Enabled = True
CheckBox12.Enabled = True
ListBox1.Clear
End Sub
Private Sub CommandButton2_Click()
MsgBox ("Ordered Successfully!")
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label2_Click()
End Sub
Private Sub Label3_Click()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Click()
End Sub`
答案 0 :(得分:0)
我认为复选框标题的数字值为标题.. 试试这段代码
Private Sub CommandButton2_Click()
Dim ctl As Control
Dim tot As Double
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
If ctl.Value = True And IsNumeric(ctl.Caption) Then
tot = tot + Val(ctl.Caption)
End If
End If
Next ctl
MsgBox "Total = " & tot & Chr(10) & "Ordered Successfully"
End Sub
答案 1 :(得分:0)
如果我理解正确,则需要声明模块级私有变量l_Chx As Byte
,并在每个复选框事件中将其增加/减少1,以获取当前复选框的数量。
答案 2 :(得分:0)
Because you add the items to a ListBox
, you can simple use ListBox1.ListCount
to return the number of items.
(Edit: The following is a general comment based on what a user would normally expect to do. However, the OP code includes disabling the checkbox when checked, so the user cannot uncheck an item.)
What is missing from your code is the case where someone unchecks an item (CheckBoxX.Value = False
in your CheckBoxX_Change
event handlers) - you must then remove the item from your ListBox
. There will be plenty of examples on the internet for "VBA removing items from a ListBox".
答案 3 :(得分:0)
I guess you want to sum up labels captions
assuming that Label1
caption has the price to be summed once CheckBox1
is checked (the same for Label2
and CheckBox2
, etc...), then you may want to:
add at the very top of your UserForm code pane
Dim total As Double
anywhere in your UserForm code pane:
Private Sub CBChange(cb As MSForms.CheckBox)
With cb
If .Value Then
Me.ListBox1.AddItem .Caption
.Enabled = False
total = total + Val(Me.Controls("label" & Replace(.Caption, "CheckBox", "")))
End If
End With
End Sub
change all your checkboxes Change
event handler as follows:
Private Sub CheckBox1_Change()
CBChange Me.ActiveControl
End Sub
Private Sub CheckBox2_Change()
CBChange Me.ActiveControl
End Sub
... and so on..
BTW you may want to change your CommandButton1_Click
event handler as follows:
Private Sub CommandButton1_Click()
Dim iCB As Long
With Me
For iCB = 1 To 12
.Controls("CheckBox" & iCB).Enabled = True
Next
.ListBox1.Clear
End With
End Sub