Excel VBA while循环

任何人都可以帮我循环?

Dim Lastrow As Long Dim i As Variant Dim Valid As Boolean Dim inputDec As Integer Lastrow = Range("D65000").End(xlUp).Row While Valid = False i = InputBox("Ve kterém sloupci je datum? (A=1,B=2,C=3, atd..)", "Datum") If IsNumeric(i) Then Valid = True Else Valid = False MsgBox ("Zadejte číslo sloupce! (A=1,B=2,C=3, atd..)") End If Wend 

你不能input字母等。但问题是,你可以input<0.我试着像CODE

 If IsNumeric(i) And ">0" Then 

但是,这并不工作,并以错误结束,任何人都可以帮助我吗?

 If IsNumeric(i) And i > 0 Then Valid = True Else Valid = False MsgBox ("Zadejte číslo sloupce! (A=1,B=2,C=3, atd..)") End If 

为了好玩,这里有另一种方法可以解决这个问题。 它不使用Valid但只是检查,如果i符合你的标准,如果没有,它显示消息,并要求input:

 Sub t2() Dim i As Variant Dim inputDec As Integer, lastRow As Integer lastRow = Range("D65000").End(xlUp).Row i = InputBox("Ve kterém sloupci je datum? (A=1,B=2,C=3, atd..)", "Datum") Do Until IsNumeric(i) And i > 0 i = InputBox("Zadejte císlo sloupce! (A=1,B=2,C=3, atd..)" & vbCrLf & vbCrLf & "Ve kterém sloupci je datum?", "Datum") Loop 'Code here will fire when `i` is Numeric and greater than 0 Debug.print "Vstup uživatele: " & i End Sub