excel vba:检查input框中的负数

我正在使用一个input框从用户获取一个数字。 我想避免不被允许的input和卡住负数。 应该处理的唯一input是1到500之间的整数。我不明白为什么-1仍然被触发。 这是我的代码到目前为止:

LBefore = InputBox("lines?", "", ">>> insert number <<<", 11660, 9540) Select Case StrPtr(LBefore) Case 0 'Cancel pressed Exit Sub Case Else If (LBefore <> "") Then 'Check for numeretical value If IsNumeric(LBefore) Then cijfer = Abs(CByte(LBefore)) 'Check to see if value is in allowed range If (cijfer >= 1) And (cijfer <= 500) Then 'do stuff ... end If End If End If End Select 

它是因为你使用cijfer = Abs(CByte(LBefore))触发的。
Abs是绝对的function,所以负数变成正数!
尝试使用cijfer = CInt(LBefore)

Interesting Posts