If Else Statement in Excel VBA

If statement in VBA is used to execute the code if a condition is true. It is also called one-way selection statement.

Syntax:-

If (condition) then
Statement
End if

For Example

Sub if_example()
If Range(\”A1\”).Value > 0 Then
Range(\”B1\”).Value = \”Positive\”
End If
End Sub


If Else Statement

The if-else statement in VBA is used to execute the code if condition is true or false. It is also called two-way selection statement.

Syntax:-

If (condition) then
Statements
Else
statement
End if

For Example


Sub if_Else_example()
If Range(\”A1\”).Value > 0 Then
Range(\”B1\”).Value = \”Positive\”
Else
Range(\”B1\”).Value = \”Negative\”
End If
End Sub