繁体   English   中英

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

[英]Always Same Custom Error Message - VBA Word 64bit

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

如果有人在格式化为整数的TextBox中输入字母,则会显示错误消息。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM