2017-04-24 184 views
0

任何人都可以幫我用我的循環?Excel VBA雖然循環

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 

不能輸入字母等。但問題是,你可以輸入< 0。我是想這樣的代碼

If IsNumeric(i) And ">0" Then 

但是,這並不工作,並在錯誤結束,可以有人幫助我嗎?

+2

'如果則IsNumeric(i)和I> 0,則'?有點不直觀,你不能只做'如果X是Y和Z',你必須做'如果X是Y而X是Z'(顯然是僞代碼)。你必須每次都充分表達情況。 – BruceWayne

+0

強制用戶輸入數字的替代方法是僅使用字母(例如'Cells(5,「D」)'是有效的),或者如果您確實需要列**數字**,則使用類似於'列( 「d」)。Column'。 – YowE3K

回答

2
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 
0

爲了好玩,這裏有另一種方法可以解決這個問題。它不使用Valid只是檢查是否符合i您的標準,如果沒有,它顯示了消息,並要求輸入:

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