Ad space (banner)
Lesson 4 / 20

Branching (the If statement)

This lesson covers how to write an If statement in VBA.

You branch with If condition Then ... ElseIf condition Then ... Else ... End If. What is distinctive is that the equivalent of else if is a single word: ElseIf.

The sample code uses If / ElseIf / Else to show one of three messages depending on score.

A common early stumble is forgetting End If. A short If that fits on one line can leave it out, but an If spread over several lines must always be closed with End If.

In professional work, If statements come up constantly for branching on the value of a cell.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)