'And' condition evaluated even if previous condition is false in VBA

Kapé

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.

Andre

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

'And' condition evaluated even if previous condition is false in VBA

From Dev

Prevent false condition from being evaluated

From Dev

Code execute even if where condition is false

From Dev

MSBuild builds target even though condition is false

From Dev

The condition is TRUE even though it should be FALSE

From Dev

Always include children even if condition is false

From Dev

Condition is not evaluated in loop

From Dev

VBA Group Rows by TRUE/FALSE Condition

From Dev

Is the condition in the for loop evaluated in every loop?

From Dev

Condition not getting properly evaluated in JavaScript

From Java

On every call of isEmailVerified() returning the false condition, Even after email verified

From Dev

How is a local variable created even when IF condition evaluates to false in Ruby?

From Dev

Statement inside If block executing even when condition is false

From Dev

While loop, extra loop even though condition is false

From Dev

Else Part is not executing even when condition is false Laravel 5.2

From Dev

While loop, extra loop even though condition is false

From Dev

sp_send_dbmail sending mail even if condition is false - SQL

From Dev

OkHttpClient.Builder Interceptor is added to retrofit 2,even condition is false

From Dev

Why is a mathematical condition not being evaluated in dynamic SQL?

From Dev

Check if a condition is false

From Dev

condition always returns false

From Dev

Why the if condition is always false?

From Dev

awk condition of true and false

From Dev

if condition always returning False

From Dev

What condition was false in the if statement

From Dev

Is there any statement/function in java which can execute the previous body if the condition becomes false?

From Dev

Print previous line if condition is met

From Dev

Returning To Previous Scene In Same Condition

From Dev

Angular 2 - Custom validator always returns true, even when condition is false

Related Related

  1. 1

    'And' condition evaluated even if previous condition is false in VBA

  2. 2

    Prevent false condition from being evaluated

  3. 3

    Code execute even if where condition is false

  4. 4

    MSBuild builds target even though condition is false

  5. 5

    The condition is TRUE even though it should be FALSE

  6. 6

    Always include children even if condition is false

  7. 7

    Condition is not evaluated in loop

  8. 8

    VBA Group Rows by TRUE/FALSE Condition

  9. 9

    Is the condition in the for loop evaluated in every loop?

  10. 10

    Condition not getting properly evaluated in JavaScript

  11. 11

    On every call of isEmailVerified() returning the false condition, Even after email verified

  12. 12

    How is a local variable created even when IF condition evaluates to false in Ruby?

  13. 13

    Statement inside If block executing even when condition is false

  14. 14

    While loop, extra loop even though condition is false

  15. 15

    Else Part is not executing even when condition is false Laravel 5.2

  16. 16

    While loop, extra loop even though condition is false

  17. 17

    sp_send_dbmail sending mail even if condition is false - SQL

  18. 18

    OkHttpClient.Builder Interceptor is added to retrofit 2,even condition is false

  19. 19

    Why is a mathematical condition not being evaluated in dynamic SQL?

  20. 20

    Check if a condition is false

  21. 21

    condition always returns false

  22. 22

    Why the if condition is always false?

  23. 23

    awk condition of true and false

  24. 24

    if condition always returning False

  25. 25

    What condition was false in the if statement

  26. 26

    Is there any statement/function in java which can execute the previous body if the condition becomes false?

  27. 27

    Print previous line if condition is met

  28. 28

    Returning To Previous Scene In Same Condition

  29. 29

    Angular 2 - Custom validator always returns true, even when condition is false

HotTag

Archive