两个值之间的VBA do-while循环不起作用

时间:2016-08-18 16:31:51

标签: excel-vba while-loop vba excel

如果myValue = 0 or myValue > nbRisk我必须循环留言 当myValue > 0 and <= nbRisk我继续 例如,如果我把myValue = 5,它再循环,当我放  myValue = 10或11 ..它不循环 有什么想法吗?

这是我的代码

Do While myValue = 0 or myValue > nbRisques
myValue = InputBox (Enter number of risk");
loop 
For i=1 to myValue

2 个答案:

答案 0 :(得分:1)

尝试以下解决方法:

' define myValue as Long
Dim myValue             As Long

Do While myValue = 0 Or myValue > nbRisques
    On Error GoTo InputBox_Err_Handler
    myValue = InputBox("Enter level of Risk to assess")

InputBox_Err_Handler:
    If Err.Number <> 0 Then
        MsgBox "Error, only Number format is allowed"
    End If
Loop

答案 1 :(得分:0)

首先,将myValue更改为变体。

然后您的代码可以更改为

Do While myValue = 0 Or myValue > nbrisques
    myValue = InputBox("Enter level of Risk to assess")
    If IsNumeric(myValue) Then
        myValue = CInt(myValue)  ' Using CInt on the assumption that you want an Integer
    Else
        myValue = 0
    End If
Loop