0

我正在使用输入框从用户那里获取数字。我想避免不允许的输入并且被负数困住。唯一应该处理的输入是 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
4

1 回答 1

3

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

于 2011-11-29T08:31:14.570 回答