Excel VBA While循环

时间:2017-04-24 20:19:32

标签: excel vba 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

你不能输入字母等。但问题是,你可以输入<我正在尝试像

这样的代码
If IsNumeric(i) And ">0" Then

但是这不起作用并以错误结束,有人可以帮我吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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