始终相同的自定义错误消息 - VBA Word 64 位

Always Same Custom Error Message - VBA Word 64bit

我正在 VBA 中为 Word 2016 64 位创建一个程序。

如果有人将字母输入到整数格式的文本框中,它会显示一条错误消息。

如何让它不允许用户输入除整数以外的任何内容?我可以让它检查用户何时单击“提交”按钮吗?这是一个文本框的代码示例:

Private Sub CurrentRate_AfterUpdate()
    If CurrentRate.Text - CurrentRate.Text > 0 Then
        CurrentRate.Value = Format(CurrentRate.Value, "Percent")

    Else
        CurrentRate.Text = CurrentRate.Text / 100
        CurrentRate.Value = Format(CurrentRate.Value, "Percent")
    End If
End Sub

基本上它是取 currentRate 的值并进行数学运算,然后将其格式化为百分比。如果它不是整数,则无法进行数学运算或将其格式化为错误原因的百分比。

在此先感谢您的帮助!

根据 Andy G 所说的,我发现我应该检查该值是否为数字,如果是公式,如果不是则显示消息框。

Private Sub CurrentRate_AfterUpdate()
    If IsNumeric(CurrentRate.Text) Then

        If CurrentRate.Text - CurrentRate.Text > 0 Then
            CurrentRate.Value = Format(CurrentRate.Value, "Percent")

        Else
            CurrentRate.Text = CurrentRate.Text / 100
            CurrentRate.Value = Format(CurrentRate.Value, "Percent")
        End If

    Else
        MsgBox("Invalid Entry, Please Try Again")
        CurrentRate.Text = ""

    End If
End Sub