1

For example:

Dim name As String: name = "NaN"

' Doesn't generate an error:
If IsNumeric(name) Then
    Dim iv As Integer: iv = CInt(name)
    If iv > 0 Then
        ' Do something
    End If
End If

' Does generate error 13 on 'CInt(name)': Types don't match
If IsNumeric(name) And CInt(name) > 0 Then
    ' Do something
End If

Why is the second condition CInt(name) > 0 even evaluated with the single if statement? Or is this just what VBA does? I'm used to write C# code which doesn't have this kind of behavior.

Community
  • 1
  • 1
Kapé
  • 4,411
  • 3
  • 37
  • 54
  • 3
    see [vba short circuit logic](http://stackoverflow.com/questions/7015471/does-the-vba-and-operator-evaluate-the-second-argument-when-the-first-is-false) – amdixon Sep 26 '15 at 11:03

1 Answers1

2

Or is this just what VBA does?

Yes.

Use this instead:

If IsNumeric(name) Then 
    If CInt(name) > 0 Then
        ' Do something
    End If
End If

or any other method from the post linked by amdixon.

Community
  • 1
  • 1
Andre
  • 26,751
  • 7
  • 36
  • 80