Excel VBA:检查输入框中的负数
我正在使用输入框从用户那里获取号码。我想避免不允许的输入并且陷入负数。唯一应该处理的输入是 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
I am using an inputbox the get a number from a user. I want to avoid not allowed input and am stuck with negative numbers. The only input which should be processed is an integer between 1 and 500. I don't understand why -1 is still triggered. Here is my code so far:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会被触发,因为您使用了
cijfer = Abs(CByte(LBefore))
。Abs
是绝对函数,所以负数变成正数!尝试使用
cijfer = CInt(LBefore)
。It's triggered because you use
cijfer = Abs(CByte(LBefore))
.Abs
is absolute function, so negative numbers become positive!Try using
cijfer = CInt(LBefore)
.