EXCEL VBA:如何使用isString或isNumeric验证值?

时间:2014-10-17 14:53:22

标签: excel vba excel-vba

以下是我的代码:

Dim m As String, n As Long

n = InputBox("Enter sales amount: ")

If n < 500 Or n > 5000 Then
ActiveCell.Value = n
ActiveCell.Interior.Color = RGB(255, 0, 0)
m = InputBox("Reason why increase/decrease? ")
ActiveCell.Offset(0, 1).Value = m
ActiveCell.Offset(1, 0).Select

Else
ActiveCell.Value = n
ActiveCell.Offset(1, 0).Select
End If

我正在弄清楚如果输入是字符串我如何验证值然后输入框会提示并要求整数? 提前感谢您的答案。

1 个答案:

答案 0 :(得分:2)

代码:

Dim m As String, n As String, sales as long
TryAgain:

n = InputBox("Enter sales amount: ")

If Not IsNumeric(n) Then
    MsgBox "Entry should be a number!"
    GoTo TryAgain
End If

sales = CLng(n)

If sales < 500 Or sales > 5000 Then
ActiveCell.Value = sales
ActiveCell.Interior.Color = RGB(255, 0, 0)
m = InputBox("Reason why increase/decrease? ")
ActiveCell.Offset(0, 1).Value = m
ActiveCell.Offset(1, 0).Select

Else
ActiveCell.Value = sales
ActiveCell.Offset(1, 0).Select
End If