excel - 将几个组合框值的总和放入单个文本框中
问题描述
一个简单的销售点系统,显示 2 个产品:Apple 和 Banana,每个价格为 10。我需要显示总金额。
我使用了 cmbApple_Change() 事件,我使用了 if elseif 语句
我只能得到 1 个组合框的结果。
Private Sub cmbApple_Change()
If cmbApple.Value = 0 Then
txtTotal.Text = 0
ElseIf cmbApple.Value = 1 Then
txtTotal.Text = 10
ElseIf cmbApple.Value = 2 Then
txtTotal.Text = 20
ElseIf cmbApple.Value = 3 Then
txtTotal.Text = 30
End If
End Sub
错误:
解决方案
这样,您将始终拥有总价值:
Private Sub cmbApple_Change()
Dim BananaItems As Byte
Dim AppleItems As Byte
On Error Resume Next
AppleItems = cmbApple.Value
BananaItems = cmbBanana.Value
On Error GoTo 0
TxtTotal.Text = AppleItems * 10 + BananaItems * 10
End Sub
Private Sub cmbBanana_Change()
Dim BananaItems As Byte
Dim AppleItems As Byte
On Error Resume Next
AppleItems = cmbApple.Value
BananaItems = cmbBanana.Value
On Error GoTo 0
TxtTotal.Text = AppleItems * 10 + BananaItems * 10
End Sub
Private Sub UserForm_Initialize()
Dim i As Byte
With cmbApple
For i = 0 To 5
.AddItem (i)
Next i
End With
With cmbBanana
For i = 0 To 5
.AddItem (i)
Next i
End With
End Sub
这很简单,但适用于您的示例。如果您需要经常更改价格,还有其他方法。
编辑:检查已编辑的答案,还更改了代码以添加项目,您可以将其更改为For i = 0 to 5
您想要的任何数字(最多 256)
推荐阅读
- python - 从命令行运行 python 模块
- angular7 - Angular:如何为关联数组做 *ngFor
- html - 有什么方法可以设置 :nth-child(an + b) 选择器的子元素?
- android - FirebaseMessaging 服务(后台服务)中的 SharedPreferences
- ruby - 无法通过终端运行 gem
- django - Django 1.9 如何使用 prefetch_related() 查询模型并获取预取数据存在的结果
- python - 在不使用 for 循环的情况下增加日期时间数组的年份
- c - 我可以在不分配缓冲区的情况下写入文件吗
- r - GetQuote:`.rowNamesDF <-`(x,value = value)中的错误:无效的'row.names'长度getsymbols
- google-bigquery - Loading Data into BigQuery: Direct Insert from Process vs Process and then loading through Google Drive?