VBA Conditional Statement IF....THEN...ELSE

The IF THEN statement is used in Excel VBA to execute a command if a particular condition is met or not. With it you can redirect your user to different part of the Excel spreadsheet or make different actions depending on the inputs in the sheet. You can display a different information in a Cell depending on what is in another cell.

Here an easy example where when you have entered all the values in the cells B5 and B6, you press the OK button. This will display in F6 the cell that is bigger

if..then.else statement

The code for this macro looks like this.

Sub Which_one_is_bigger()

If Range("B5").Value > Range("b6").Value Then
Range("F6").Value = "B5 is bigger"
Else
Range("f6").Value = "B6 is bigger"
End If

End Sub
This is the basic concept for the IF THEN ELSE operation. This conditional statement exist in Excel formulas too, you can see my tutorial here but for complex operations, doing it with visual basic can be quite easier.

So you do not need to use the ELSE statement. You can just have a IF THEN statement if you need. Then you also do not need the END IF line.

 

This should give you enough to start programming message boxes in VBA by yourself. Good Luck.

 

Please Tweet, Like or Share us if you enjoyed.