在满足特定限制后禁用组合框

时间:2015-01-01 14:47:39

标签: vba

我有4个组合框叫做:

cboOption1 
cboOption2 
cboOption3 
cboOption4

每个值都可以有2个值中的一个,10或20个点,用户从下拉列表中选择。但是,用户只能在所有下拉列表中选择最多40个点,之后将禁用随后的剩余下拉列表。

E.g.1

cboOption1 = 20
cboOption2 = 20

然后

cboOption3.Enabled = False
cboOption3.Enabled = False 

E.g.2

cboOption1 = 10
cboOption2 = 20
cboOption3 = 10

然后

cboOption4.Enabled = False 

E.g.3

cboOption1 = 10
cboOption2 = 10
cboOption3 = 10
cboOption3 = 40

然后

None disabled

我的问题

我目前遇到的困难是弄清楚如何根据用户输入来打开/关闭组合框。到目前为止,这是我的代码,它似乎没有工作得太好,任何想法如何改进它。

到目前为止

代码

Sub checkTotal(var1, var2, var3, var4, total)
so1 = Int(var1)
so2 = Int(var2)
so3 = Int(var3)
so4 = Int(var4)
total = Nz(so1, 0) + Nz(so2, 0) + Nz(so3, 0) + Nz(so4, 0)

If total > 40 And (so1 = "20" Or so2 = "20" Or so3 = "20" Or so4 = "20") Then
   cboOption3.Value = ""
   cboOption3.Enabled = False
   cboOption4.Value = ""
   cboOption4.Enabled = False
End If

MsgBox total
End Sub

Private Sub cboOption1_Change()
Call checkTotal(cboOption1.Column(1), cboOption2.Column(1), cboOption3.Column(1), cboOption4.Column(1), so1RunningTotal)
End Sub

Private Sub cboOption2_Change()
Call checkTotal(cboOption1.Column(1), cboOption2.Column(1), cboOption3.Column(1), cboOption4.Column(1), so1RunningTotal)
End Sub

Private Sub cboOption3_Change()
Call checkTotal(cboOption1.Column(1), cboOption2.Column(1), cboOption3.Column(1), cboOption4.Column(1), so1RunningTotal)
End Sub

Private Sub cboOption4_Change()
Call checkTotal(cboOption1.Column(1), cboOption2.Column(1), cboOption3.Column(1), cboOption4.Column(1), so1RunningTotal)
End Sub

1 个答案:

答案 0 :(得分:2)

请阅读我对这个问题的评论。我建议这样做:

cboOption3.Enabled = CBool((cbo1Value + cbo2Value)<40)
cboOption4.Enabled = CBool((cbo1Value + cbo2Value + cbo3Value)<40)

提示:使用正确的输入参数数据类型,并从输入参数中删除总计

Sub checkTotal(ByVal var1 As Integer, ByVal var2 As Integer, ByVal var3 As Integer, ByVal var4 As Integer)

请参阅:Data Type Summary (Visual Basic)