我收到以下错误消息:
程序太大
我认为VBA的每个程序限制为64k。如何拆分此代码或将其缩短?
If txt29.Value = "" Then
Box67.BackColor = labelnull.BackColor
cmd29.Enabled = False
Else
cmd29.Enabled = True
End If
If txt29.Value = "Yellow" Then
Box67.BackColor = LabelYellow.BackColor
End If
If txt29.Value = "Blue" Then
Box67.BackColor = LabelBlue.BackColor
End If
If txt29.Value = "Red" Then
Box67.BackColor = LabelRed.BackColor
End If
If txt29.Value = "Brown" Then
Box67.BackColor = lableBrown.BackColor
End If
If txt29.Value = "Pink" Then
Box67.BackColor = Labelpink.BackColor
End If
If txt29.Value = "White" Then
Box67.BackColor = Labelpink.BackColor
End If
If txt29.Value = "Grey" Then
Box67.BackColor = Labelpink.BackColor
End If
If txt29.Value = "Purple" Then
Box67.BackColor = Labelpink.BackColor
End If
If txt29.Value = "Green" Then
Box67.BackColor = LabelGreen.BackColor
End If
我需要和上面的100个按钮一样
答案 0 :(得分:0)
您可以创建10个功能,每个功能设置10个控件。
然后从一个" master"中调用这十个函数。功能
答案 1 :(得分:0)
您也可以使用Select Case来缩短代码:
If txt29.Value = "" Then
Box67.BackColor = labelnull.BackColor
cmd29.Enabled = False
Else
cmd29.Enabled = True
End If
Select Case txt29.value
Case "Yellow"
Box67.BackColor = LabelYellow.BackColor
Case "Blue"
Box67.BackColor = LabelBlue.BackColor
Case "Red"
Box67.BackColor = LabelRed.BackColor
Case "Brown"
Box67.BackColor = lableBrown.BackColor
Case "Pink"
Box67.BackColor = Labelpink.BackColor
Case "White"
Box67.BackColor = Labelpink.BackColor
Case "Grey"
Box67.BackColor = Labelpink.BackColor
Case "Purple"
Box67.BackColor = Labelpink.BackColor
Case "Green"
Box67.BackColor = LabelGreen.BackColor
End Select
如果价值恰好是"黄色"
,这也会加快速度,因为它不必通过每个选项。答案 2 :(得分:0)
我打算说只是用正确的名称引用标签并查看它的背面颜色 - 但它不适用于Pink标签:
If IsNull(txt29.Value) Then
box67.BackColor = labelnull.BackColor
Else
box67.BackColor = Me.Controls("Label" & txt29.Value).BackColor
End If
如果代码试图引用不存在的标签,它也会导致问题。