2011-11-29 38 views
0

我正在使用inputbox从用户获取一个数字。我想避免不允许的输入,并坚持负数。应该处理的唯一输入是1到500之间的整数。我不明白为什么-1仍然被触发。这里是我的代码到目前为止:excel vba:检查输入框中的负数

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 

回答

3

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

+0

就是这样。谢谢。 – user366121