VBA - Conditional Flow

A key concept of programming is conditional flow. This is where the program will compare a value or and range of value to determine the flow path of the program.

A basic example of an if statement will perform a task IF a statement equates to true.


We can include an else statement that will execute if the expression equates to false.




 If we want to compare a variable to a range of values then we can use the ElseIf keyword. This allows us to evaluated a range of statements to determine the flow of the program.


Comparisons

> - Greater than
< - Less Than
>= - Greater or equal to
<= - Less than or equal to
= - Equal to
<> - Not equal to

These can be combined with AND and OR statements


Code Snippet:

Sub a5_conditional_flow_b()

Dim testScore As Integer

testScore = 60

If testScore >= 50 And testScore < 100 Then
    MsgBox "Pass"
ElseIf testScore < 0 Then
    MsgBox "Did something go wrong?"
Else
    MsgBox "Try again"
End If

End Sub




Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.



Must Read Articles


VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.