Mára Brchy
Mára Brchy

Reputation: 21

If logic for value being numeric and greater than zero

I need help fixing the If logic below:

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

You can't enter letters etc..

The problem is, that you can enter < 0.

I tried

If IsNumeric(i) And ">0" Then

That ended in error.

Upvotes: 2

Views: 329

Answers (2)

gnub
gnub

Reputation: 193

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

Upvotes: 2

BruceWayne
BruceWayne

Reputation: 23283

For fun, here's another way you can tackle this. It doesn't use Valid but just checks if i matches your criteria, and if it doesn't, it shows the message and asks for an 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

Upvotes: 0

Related Questions