You’ve come to the right place of simple solutions & useful tricks in VBA in Excel, other Office applications (Word, PowerPoint…) and external apps (SAP, Internet Explorer…) too!
I invite you to Posts to read the latest entries. Use search engine to find articles You’re interested in from this website.
If You are better watcher go to Videos to watch some tutorials of mine from my YouTube channel.
Visit Memes gallery to see my collection of articles main pictures, also stored on Pinterest profile.
Go to About me to contact me.
Recent posts:
Working with VBA for years, I’ve seen the same performance killers over and over again. In this article, I’m going to show you the 5 most common VBA mistakes that make your code crawl like a snail, and how to fix them to get lightning-fast performance. I’ll prove each point with real examples and performance tests, so you can see the dramatic difference yourself. This is probably the biggest performance killer I see in VBA code. When Excel updates the screen after every single operation, it slows everything down massively. Performance Test Results: Reading and writing to Excel cells one by one is incredibly slow. Arrays are your best friend for bulk operations. Performance Test Results (10,000 rows): Using Performance Test Results: This is a classic mistake from recorded macros. Selecting objects is completely unnecessary and slow. Performance Test Results: Every time you reference an object, VBA has to resolve that reference. With statements cache the reference. Performance Test Results: Here’s how to combine all these optimizations into one super-fast template: Testing all optimizations on 10,000 rows of data: These simple changes can transform your slow VBA code into lightning-fast automation. The difference is not just noticeable – it’s dramatic! Try implementing these optimizations in your next VBA project. You’ll be amazed at how much faster your code runs. What’s your biggest VBA performance challenge? Let me know in the comments, and I might cover it in the next article! I already showed You how to get data from Excel file using ListObject and using SQL query in VBA. To make things more interesting I thought it is time for something way more different. In this article I’m going to shortly present how to get Excel data using Python.How to Debug VBA Like a Professional – Complete Guide (2025)
MsgBox statements everywhere! Learn the professional way to find and fix bugs quickly.
Why Professional Debugging Matters
I’ve seen developers spend hours adding MsgBox or Debug.Print statements, running code, removing them, and repeating the process. This is the slowest way to debug.
Professional debugging tools let you:
Tool #1: Breakpoints – Your Best Friend
Breakpoints pause your code execution at a specific line. This is the most powerful debugging tool in VBA.
How to Set Breakpoints:
Sub DebugExample()
Dim i As Long
Dim total As Double
total = 0
For i = 1 To 100 ' Set breakpoint here (F9)
total = total + i
Next i
MsgBox "Total: " & total
End Sub💡 Pro Tip: When execution pauses at a breakpoint, you can hover your mouse over any variable to see its current value instantly!
Types of Breakpoints:
1. Regular Breakpoint (F9): Pauses every time the line is reached
2. Conditional Breakpoint: Right-click on a breakpoint → Add Watch → Set condition
For i = 1 To 1000
' Set watch: Break when i = 500
total = total + i
Next iTool #2: Stepping Through Code
Once paused at a breakpoint, you can step through your code line by line to see exactly what’s happening.
Keyboard Shortcuts (Learn These!):
Shortcut
Action
When to Use
F8
Step Into
Execute next line, enters functions
Shift+F8
Step Over
Execute next line, skips functions
Ctrl+Shift+F8
Step Out
Exit current function
F5
Continue
Run until next breakpoint
Ctrl+F8
Run to Cursor
Run until cursor position
Practical Example:
Sub StepThroughDemo()
Dim result As Double
result = CalculateTotal(100) ' Press F8 here to step INTO function
' Or Shift+F8 to step OVER function
MsgBox "Result: " & result
End Sub
Function CalculateTotal(ByVal maxValue As Long) As Double
Dim i As Long
Dim sum As Double
sum = 0
For i = 1 To maxValue
sum = sum + i
Next i
CalculateTotal = sum
End Function⚡ Quick Tip: Use F8 (Step Into) when you want to debug inside functions. Use Shift+F8 (Step Over) when you trust the function and just want to see the result.
Tool #3: Immediate Window – Your Debug Console
The Immediate Window is like a live VBA console. You can execute commands, test expressions, and print values while your code is paused.
Open Immediate Window:
What You Can Do:
1. Print Variable Values:
2. Change Variables on the Fly:
? variableName ' Prints value
? i ' Shows current value of i
? total * 2 ' Evaluates expressions
3. Execute Any VBA Code:
i = 500 ' Change i to 500 while debugging
total = 0 ' Reset total to 0Call MyFunction() ' Run any function
MsgBox "Test" ' Show message box
Range("A1").Value = "Test" ' Modify Excel directlyReal-World Example:
Sub ImmediateWindowDemo()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets(1)
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row ' Set breakpoint here
' In Immediate Window, type:
' ? lastRow (see the value)
' ? ws.Name (see worksheet name)
' ? ws.Range("A1").Value (see cell value)
' lastRow = 10 (change it!)
End Sub💡 Pro Tip: Use
Debug.Print in your code to send output to the Immediate Window without stopping execution!Tool #4: Watch Expressions – Monitor Variables
Watch Expressions let you monitor specific variables or expressions automatically as your code runs.
How to Add Watch:
Types of Watches:
1. Watch Expression: Just monitors the value
2. Break When Value Is True: Pauses when condition becomes True
3. Break When Value Changes: Pauses when variable changes
Sub WatchExpressionDemo()
Dim i As Long
Dim arr(1 To 100) As Long
For i = 1 To 100
arr(i) = i * 2
' Add watch: arr(50) - monitor specific array element
' Add watch: i > 75 - break when condition is true
Next i
End Sub⚠️ Warning: Too many watch expressions can slow down debugging. Use them strategically on problematic variables only.
Tool #5: Locals Window – See Everything
The Locals Window shows all variables in the current scope automatically. No setup needed!
Open Locals Window:
Sub LocalsWindowDemo()
Dim ws As Worksheet
Dim rng As Range
Dim i As Long
Dim total As Double
Set ws = ThisWorkbook.Sheets(1)
Set rng = ws.Range("A1:A10")
' Set breakpoint here - check Locals Window
' You'll see ws, rng, i, total all listed with values
For i = 1 To 10
total = total + rng.Cells(i, 1).Value
Next i
End SubTool #6: Professional Error Handling
Professional VBA code doesn’t crash – it handles errors gracefully.
Basic Error Handling:
Sub ErrorHandlingBasic()
On Error GoTo ErrorHandler
Dim ws As Worksheet
Dim result As Double
Set ws = ThisWorkbook.Sheets("DataSheet") ' Might not exist
result = 100 / 0 ' Division by zero
MsgBox "Success!"
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Debug.Print "Error in ErrorHandlingBasic: " & Err.Description
End SubAdvanced Error Handling:
Sub ErrorHandlingAdvanced()
On Error GoTo ErrorHandler
Dim ws As Worksheet
Dim filePath As String
filePath = "C:\Data\Report.xlsx"
' Your code here
Set ws = ThisWorkbook.Sheets("Sales")
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 9 ' Subscript out of range
MsgBox "Worksheet 'Sales' not found!", vbCritical
Debug.Print "Missing worksheet error at " & Now()
Case 53 ' File not found
MsgBox "File not found: " & filePath, vbExclamation
Debug.Print "File error: " & filePath
Case Else
MsgBox "Unexpected error: " & Err.Description, vbCritical
Debug.Print "Error " & Err.Number & ": " & Err.Description
End Select
' Clean up or retry logic here
End SubError Handling Best Practices:
The Complete Professional Debugging Workflow
Here’s my step-by-step debugging process for any VBA issue:
Sub ProfessionalDebuggingTemplate()
On Error GoTo ErrorHandler
' 1. Declare variables with proper types
Dim ws As Worksheet
Dim dataArray As Variant
Dim i As Long
Dim lastRow As Long
' 2. Open Locals Window (View → Locals)
' 3. Open Immediate Window (Ctrl+G)
Set ws = ThisWorkbook.Sheets(1)
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' 4. Set breakpoint here (F9)
dataArray = ws.Range("A1:A" & lastRow).Value
' 5. Step through with F8
For i = 1 To UBound(dataArray, 1)
' 6. In Immediate Window, type: ? dataArray(i, 1)
' 7. Add Watch on: i > 50
dataArray(i, 1) = dataArray(i, 1) * 2
Next i
ws.Range("B1:B" & lastRow).Value = dataArray
Exit Sub
ErrorHandler:
' 8. Professional error handling
Debug.Print "Error at " & Now() & ": " & Err.Description
MsgBox "Error: " & Err.Description, vbCritical, "Debug Info"
End SubDebugging Keyboard Shortcuts Cheat Sheet
Shortcut
Action
F8
Step Into (enter functions)
Shift+F8
Step Over (skip functions)
Ctrl+Shift+F8
Step Out (exit function)
F9
Toggle Breakpoint
Ctrl+Shift+F9
Clear All Breakpoints
Ctrl+G
Open Immediate Window
F5
Continue Execution
Ctrl+F8
Run to Cursor
Key Takeaways
Professional debugging isn’t about being a better programmer – it’s about using better tools. These built-in VBA debugging features will save you hours of frustration and make you a more efficient developer.
Stop adding MsgBox statements everywhere. Start debugging like a pro!
What’s your biggest VBA debugging challenge? Share in the comments below, and I’ll help you solve it!
5 Most Common VBA Mistakes That Slow Down Your Code
Mistake #1: Not Turning Off Screen Updating
The Wrong Way:
Sub SlowExample()
Dim i As Long
For i = 1 To 1000
Cells(i, 1).Value = i
Cells(i, 2).Value = i * 2
Next i
End SubThe Right Way:
Sub FastExample()
Dim i As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = 1 To 1000
Cells(i, 1).Value = i
Cells(i, 2).Value = i * 2
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
• Slow version: 2.3 seconds
• Fast version: 0.1 seconds
• Speed improvement: 23x faster!Mistake #2: Using Cells/Range in Loops Instead of Arrays
The Wrong Way:
Sub SlowCellAccess()
Dim i As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
For i = 1 To 10000
ws.Cells(i, 1).Value = ws.Cells(i, 1).Value * 2
Next i
End SubThe Right Way:
Sub FastArrayAccess()
Dim i As Long
Dim ws As Worksheet
Dim dataArray As Variant
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets(1)
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' Read data to array
dataArray = ws.Range("A1:A" & lastRow).Value
' Process in memory
For i = 1 To UBound(dataArray, 1)
dataArray(i, 1) = dataArray(i, 1) * 2
Next i
' Write back to Excel
ws.Range("A1:A" & lastRow).Value = dataArray
End Sub
• Slow version: 8.7 seconds
• Fast version: 0.2 seconds
• Speed improvement: 43x faster!Mistake #3: Not Using Proper Variable Types
Variant for everything is convenient but slow. VBA has to figure out what type of data it’s dealing with every time.The Wrong Way:
Sub SlowVariantExample()
Dim i, j, total
Dim ws
Set ws = ThisWorkbook.Sheets(1)
total = 0
For i = 1 To 1000
For j = 1 To 10
total = total + ws.Cells(i, j).Value
Next j
Next i
End SubThe Right Way:
Sub FastTypedExample()
Dim i As Long, j As Long, total As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
total = 0
For i = 1 To 1000
For j = 1 To 10
total = total + ws.Cells(i, j).Value
Next j
Next i
End Sub
• Slow version: 1.8 seconds
• Fast version: 0.9 seconds
• Speed improvement: 2x faster!Mistake #4: Selecting and Activating Objects
The Wrong Way:
Sub SlowSelectExample()
Dim i As Long
For i = 1 To 100
Range("A" & i).Select
Selection.Value = "Data " & i
Selection.Font.Bold = True
Next i
End SubThe Right Way:
Sub FastDirectExample()
Dim i As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
For i = 1 To 100
With ws.Range("A" & i)
.Value = "Data " & i
.Font.Bold = True
End With
Next i
End Sub
• Slow version: 3.1 seconds
• Fast version: 0.4 seconds
• Speed improvement: 7.7x faster!Mistake #5: Not Using With Statements Properly
The Wrong Way:
Sub SlowObjectAccess()
Dim i As Long
For i = 1 To 1000
ThisWorkbook.Sheets(1).Cells(i, 1).Value = i
ThisWorkbook.Sheets(1).Cells(i, 1).Font.Bold = True
ThisWorkbook.Sheets(1).Cells(i, 1).Interior.Color = RGB(255, 255, 0)
Next i
End SubThe Right Way:
Sub FastWithStatement()
Dim i As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
For i = 1 To 1000
With ws.Cells(i, 1)
.Value = i
.Font.Bold = True
.Interior.Color = RGB(255, 255, 0)
End With
Next i
End Sub
• Slow version: 4.2 seconds
• Fast version: 1.1 seconds
• Speed improvement: 3.8x faster!The Ultimate Fast Code Template
Sub UltimateFastCode()
' Variable declarations with proper types
Dim ws As Worksheet
Dim dataArray As Variant
Dim i As Long, j As Long
Dim lastRow As Long, lastCol As Long
' Set worksheet reference
Set ws = ThisWorkbook.Sheets(1)
' Turn off Excel overhead
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
' Get data dimensions
With ws
lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
End With
' Read data to array (single operation)
dataArray = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol)).Value
' Process data in memory
For i = 1 To UBound(dataArray, 1)
For j = 1 To UBound(dataArray, 2)
' Your processing logic here
dataArray(i, j) = dataArray(i, j) * 1.1
Next j
Next i
' Write back to Excel (single operation)
ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol)).Value = dataArray
' Restore Excel settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End SubPerformance Comparison Summary
Method
Time (seconds)
Speed Improvement
All mistakes combined
15.2
Baseline
All optimizations
0.3
50x faster!
Key Takeaways
How to get Excel data using Python?
