停止在VBA中自动刷新的文本框
我目前遇到一个问题。 我在文本框中设置了一个if语句 – 如果数字小于2.4,大于6,则返回错误。
但是,如果用户希望input2.8,当input2时会抛出错误,而当“。” 被input。
这是我的代码:
Private Sub txtHeight_Change() HeightNumber = CDbl(Val(Me.txtHeight.Value)) If Not Me.txtHeight = "" Then If HeightNumber >= 2.4 And HeightNumber <= 6 Then Else MsgBox ("You have entered an incorrect number. Please enter a number between 2.4m and 6m.") End If End If totcost = (HeightNumber * Width1) * (painttype + undercost) TotalCost.Value = totcost End Sub
不要使用txtHeight_Change()
。 使用_AfterUpdate()
事件。
这是你正在尝试?
Private Sub txtHeight_AfterUpdate() If Len(Trim(txtHeight.Value)) = 0 Then Exit Sub If HeightNumber >= 2.4 And HeightNumber <= 6 Then HeightNumber = CDbl(Val(txtHeight.Value)) totcost = (HeightNumber * Width1) * (painttype + undercost) TotalCost.Value = totcost Else MsgBox ("You have entered an incorrect number. Please enter a number between 2.4m and 6m.") End If End Sub