Excel_VBA_Introduction_Post_Course_Reference
Excel_VBA_Introduction_Post_Course_Reference
IF Statement
The IF statement is used to execute code based on whether a condition is true or false. Here’s a basic example:
Sub CheckValue()
Dim score As Integer
score = 75
In this example, the IF statement checks the value of score and displays a message box with the corresponding grade.
Sub CheckDay()
Dim dayOfWeek As String
dayOfWeek = "Wednesday"
In this example, the SELECT CASE statement checks the value of dayOfWeek and displays a message box with a
corresponding message.
Both structures help control the flow of your VBA code based on different conditions.
Sub BasicDoLoop()
counter = 0
In this code, the loop will run as long as counter is less than 10. Each time the loop runs, counter is incremented by 1 and
its value is printed in the Immediate Window.
Sub DoLoopWithIf()
counter = 0
Do
counter = counter + 1
In this example, the loop runs while counter is less than 10. Inside the loop, the If statement checks if counter is even or
odd and prints the result.
Sub DoLoopWithExit()
counter = 0
Do
counter = counter + 1
If counter = 5 Then
Exit Do
End If
Debug.Print counter
Loop While counter < 10
End Sub
In this code, the loop will exit when counter equals 5, even though the condition to continue looping is counter < 10.
Excel VBA Introduction
More DO LOOP examples
This example demonstrates a Do...Loop that runs a fixed number of times:
Sub SimpleDoLoop()
Do
Debug.Print "Hello, World!"
Loop Until False
End Sub
In this code, the loop will run indefinitely because the condition Until False is never met. To stop it, you would need to
manually interrupt the execution.
Sub DoLoopWithIf()
Do
If Now > #12:00:00 PM# Then
Debug.Print "It's past noon!"
Exit Do
End If
Loop
End Sub
In this example, the loop will keep running until the current time is past noon. Once the condition is met, it prints
a message and exits the loop.
Sub DoLoopWithExit()
Do
Debug.Print "Running..."
Exit Do
Loop
End Sub
In this code, the loop will run only once because Exit Do is called immediately, breaking out of the loop.
Here’s an example of a Do Until loop in VBA that continues to run until the active cell’s value is an empty string:
Sub DoUntilActiveCellEmpty()
Do Until ActiveCell.Value = ""
' Perform some action
Debug.Print ActiveCell.Value
Explanation:
Do Until ActiveCell.Value = “”: The loop will continue to run until the active cell’s value is an empty string.
Debug.Print ActiveCell.Value: This line prints the value of the active cell in the Immediate Window.
ActiveCell.Offset(1, 0).Select: This moves the active cell one row down.
This loop will keep moving down the column, printing each cell’s value, until it encounters an empty cell.
Excel VBA Introduction
The FOR NEXT loop and the FOR EACH loop
Let’s dive into the FOR NEXT and FOR EACH loops in Excel VBA with some examples.
Loop Through Excel Worksheets and Workbooks
This loop will display a message box five times, showing the values
from 1 to 5.
This loop will display a message box for the values 1, 3, 5, 7, and 9.
This loop will display a message box for each worksheet in the workbook, showing the name of each worksheet.
Excel VBA Introduction
Example 2: FOR EACH Loop with an Array
Sub ForEachArray()
Dim arr As Variant
Dim element As Variant
arr = Array(1, 2, 3, 4, 5)
For Each element In arr
MsgBox "The value of the element is " & element
Next element
End Sub
This loop will display a message box for each element in the array,
showing the values 1, 2, 3, 4, and 5.
Debugging Tools
Here are the key debugging tools in Excel VBA:
Breakpoints
Purpose: Halt the execution of your code at a specific line.
How to Use: Click in the left margin next to the line of code where you want to set the breakpoint. A red dot will appear.
When you run the macro, it will stop at this line, allowing you to inspect the state of your program.
Immediate Window
Purpose: Test code snippets and evaluate expressions on the fly.
How to Use: Open the Immediate Window by pressing Ctrl+G.
You can type commands and see their results immediately.
Watch Window
Purpose: Monitor the values of variables and expressions as
your code runs.
Ask questions on our
How to Use: Right-click on a variable and select
“Add Watch” to track its value during execution.
post course learning
Locals Window
support forum
Purpose: Display all local variables and their values.
How to Use: Open the Locals Window from the View menu.
Log in using your email
It updates automatically as you step through your code.
and your post course
Call Stack
Purpose: View the sequence of procedure calls that led to
email when you
the current point in your code.
How to Use: Open the Call Stack window from the Debug
completed the feedback
menu to see the list of active procedures.
These tools can significantly help you understand and fix
issues in your VBA code.