在VBA中自动刷新停止文本框

时间:2014-07-16 14:32:07

标签: excel vba excel-vba

我目前遇到了问题。我在文本框上设置了if语句 - 如果数字小于2.4,并且大于6则返回错误。

但是,如果用户希望输入2.8,则在输入2时会出现错误,并且当'。'进入。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

请勿使用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