🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →

The ExcelMaster.ai Blog

VBA WorksheetFunction in Excel — Call Excel's Own Functions from Code (and the Two Ways They Fail)

VBA WorksheetFunction in Excel — Call Excel's Own Functions from Code (and the Two Ways They Fail)

Application.WorksheetFunction lets VBA borrow Excel's 450-plus built-in functions instead of rewriting SUM, VLOOKUP or COUNTIF as a loop. The catch is that there are two ways to call them and they fail differently. WorksheetFunction.X raises a run-time error when there is no match, while Application.X returns an error value you test with IsError. Learn which call style to use, why the result must be a Variant, which functions you should not call this way, and when to pass a whole range instead of looping cell by cell.

Henry
VBA Remove Duplicates in Excel — Dedupe in One Line (and Why It Deletes With No Undo)

VBA Remove Duplicates in Excel — Dedupe in One Line (and Why It Deletes With No Undo)

Range.RemoveDuplicates is Excel's Remove Duplicates button in a single line of code, but it is destructive in ways a loop is not. It deletes rows in place, keeps the first occurrence, and cannot be undone after a macro runs. The argument that trips everyone is Columns, whose numbers are offsets inside the range, not sheet column letters. Learn the range-offset trap, why Header xlYes matters, how to keep the last row instead of the first, and when to reach for Advanced Filter or a Dictionary instead.

Henry
VBA Advanced Filter in Excel — Extract Unique Values and Filter to a New Range (Without a Loop)

VBA Advanced Filter in Excel — Extract Unique Values and Filter to a New Range (Without a Loop)

Range.AdvancedFilter is the one filter that outputs data instead of a view. It can pull a unique list or a criteria-matched set of rows to another location in a single call, without a loop and without deleting anything. The part that feels alien is that its WHERE clause lives in cells, a criteria range whose header must match the data exactly. Learn xlFilterInPlace versus xlFilterCopy, how the criteria range works, how Unique True dedupes non-destructively, the header-match trap that returns empty output, and how it differs from AutoFilter and Remove Duplicates.

Henry
VBA Find in Excel — Search Cells the Right Way (It Returns a Range, Not a Position)

VBA Find in Excel — Search Cells the Right Way (It Returns a Range, Not a Position)

The VBA Range.Find method is Excel's Ctrl+F for code, and it trips people up because it returns a Range object (or Nothing when there is no match) instead of a number. Learn the one check that stops the error-91 crash, the sticky-arguments trap that makes Find behave differently every run, how LookAt xlWhole versus xlPart decides whole-cell versus contains, and how to loop FindNext to get every match without an infinite loop.

Henry
VBA AutoFilter in Excel — Filter Rows in Code (and Why Hidden Isn't Deleted)

VBA AutoFilter in Excel — Filter Rows in Code (and Why Hidden Isn't Deleted)

The VBA AutoFilter method filters a table in code, but the rows it hides are still there — they still count in SUM, still copy, and still sit in your range. Learn the mental model that prevents the classic bugs, why you must go through SpecialCells xlCellTypeVisible to touch only visible rows, how criteria and operators work, the toggle trap that turns your filter off on the second run, and the fast filter-then-delete pattern for bulk row removal.

Henry
VBA Sort in Excel — Range.Sort vs the Sort Object (and How to Get the Original Order Back)

VBA Sort in Excel — Range.Sort vs the Sort Object (and How to Get the Original Order Back)

Sorting in VBA is a permanent reorder with no Ctrl+Z, so the first rule is to protect the original order before you touch it. Learn the mental model that separates a view from a mutation, why Header xlYes matters or your titles end up in the data, the difference between the quick Range.Sort and the unlimited Sort object, the SortFields.Clear trap that inherits old sort keys, and why numbers stored as text sort in the wrong order.

Henry
VBA Delete Rows in Excel — Delete Rows, Blank Rows and Rows by Condition (Loop Backwards!)

VBA Delete Rows in Excel — Delete Rows, Blank Rows and Rows by Condition (Loop Backwards!)

Deleting a row in VBA is a structural edit, not a clear — every row below slides up to fill the gap, which is why a forward loop skips rows. Learn the one rule that fixes it (loop bottom to top, or delete in a single Union), how EntireRow.Delete differs from clearing a cell, the fast way to strip blank rows, and how to delete rows by condition without corrupting your data or reference formulas.

Henry
VBA Insert Rows and Columns in Excel — Shift the Grid the Right Way (and Insert in a Loop Without Chaos)

VBA Insert Rows and Columns in Excel — Shift the Grid the Right Way (and Insert in a Loop Without Chaos)

Inserting is deleting's mirror — it pushes existing rows down (or columns right) to make room, so the same shifting that breaks a delete loop breaks an insert loop too. Learn EntireRow.Insert versus a partial range with the Shift argument, why CopyOrigin decides which neighbour's formatting the new row inherits, the safe direction to insert inside a loop, and the one-call way to add many rows at once.

Henry
VBA Hide Columns and Rows in Excel — Hidden Isn't Deleted (and Why Your Totals Don't Change)

VBA Hide Columns and Rows in Excel — Hidden Isn't Deleted (and Why Your Totals Don't Change)

Hiding a column in VBA is not deleting it and not filtering it — the data is still there, still in every SUM, still copied by a range copy, just given zero display width. Learn why .Hidden lives on EntireColumn and EntireRow, why hidden cells still count in formulas, the unhide-everything line that rescues a stuck sheet, and how manually hidden rows differ from AutoFilter-hidden rows when you loop.

Henry
VBA Worksheet_BeforeDoubleClick in Excel — Turn a Double-Click Into an Action (and Suppress Edit Mode)

VBA Worksheet_BeforeDoubleClick in Excel — Turn a Double-Click Into an Action (and Suppress Edit Mode)

Worksheet_BeforeDoubleClick is the event Excel fires the instant you double-click a cell, before it enters edit mode, and it hands you the Target cell plus a Cancel flag. Set Cancel to True to suppress edit mode and run your own action instead — toggle a checkmark, mark a row done, or drill down to detail. Learn how to scope it to one column with Intersect, why forgetting Cancel drops the cell into edit mode, and where the code must live.

Henry
VBA Worksheet_BeforeRightClick in Excel — Replace the Right-Click Menu (and Why Disabling It Isn't Security)

VBA Worksheet_BeforeRightClick in Excel — Replace the Right-Click Menu (and Why Disabling It Isn't Security)

Worksheet_BeforeRightClick is the event Excel fires the instant you right-click a cell, before the context menu appears, and it hands you the Target cell plus a Cancel flag. Set Cancel to True to suppress the built-in menu and run your own action or show a custom menu instead. Learn how to scope it with Intersect so you don't cripple Copy and Paste, why disabling right-click is UX rather than protection, and where the code must live.

Henry
VBA Workbook_BeforePrint in Excel — Block a Print, Stamp a Header, and the Print-Preview Gotcha

VBA Workbook_BeforePrint in Excel — Block a Print, Stamp a Header, and the Print-Preview Gotcha

Workbook_BeforePrint is the event Excel fires before anything in the workbook is printed, and it hands you a Cancel flag. Set Cancel to True and the print is blocked — validate before printing, or stop a draft going out. Learn why the event fires for Print Preview too (so heavy work makes preview crawl), why it runs once for the whole workbook rather than per sheet, why a silent Cancel is a bug, and where the code must live.

Henry
VBA Worksheet_Activate and Deactivate in Excel — Run Code When You Switch Sheets (and Why You Can't Cancel a Leave)

VBA Worksheet_Activate and Deactivate in Excel — Run Code When You Switch Sheets (and Why You Can't Cancel a Leave)

Worksheet_Activate fires when a sheet becomes the active one and Worksheet_Deactivate fires just as you leave it — the arrival and departure events for a sheet. The catch that defines them — unlike BeforeClose and BeforeSave, neither gives you a Cancel, so you can watch a sheet switch but you can't block it. Learn the refresh-on-view pattern, the bounce-back workaround, and sheet-level versus workbook-level handlers.

Henry
VBA Class Module in Excel — Build Your Own Object (Blueprint vs Instance, and the As New Trap)

VBA Class Module in Excel — Build Your Own Object (Blueprint vs Instance, and the As New Trap)

A VBA class module lets you define your own object type — a blueprint that bundles data and behaviour — then stamp out independent instances with New. The catch that trips everyone is that objects are reference types, so Set b = a makes both names point at the same instance, and Dim x As New hides a lazy-instantiation trap. Learn how the module name becomes the type name, when a class beats a Type, and how to avoid the classic New pitfalls.

Henry
VBA Type in Excel — Group Related Fields Into One Variable (User-Defined Types vs a Class)

VBA Type in Excel — Group Related Fields Into One Variable (User-Defined Types vs a Class)

A VBA Type — a user-defined type declared with Type ... End Type — bundles several related fields into one variable, so Name, Age and Salary travel together instead of as three parallel arrays. It is a value type, which means assigning one Type variable to another copies every field, unlike objects, which share. Learn where the declaration must live, why copy-not-share matters, and the exact point where you should reach for a class module instead.

Henry
VBA Property in Excel — Get, Let and Set (Controlled Access to a Class's Fields)

VBA Property in Excel — Get, Let and Set (Controlled Access to a Class's Fields)

Property Get, Let and Set turn a class field into a gate — a small procedure that runs your code whenever the outside reads or writes it, so you can validate input, compute values on the fly, or make a field read-only. The distinction that trips everyone is Let versus Set — Let assigns a value, Set assigns an object — and using the wrong one is a compile or run-time error. Learn the backing-field pattern, when a plain Public variable is the honest choice, and how to build a read-only property.

Henry
VBA ActiveCell in Excel — The One Cell With the Cursor (ActiveCell vs Selection, and When It Breaks)

VBA ActiveCell in Excel — The One Cell With the Cursor (ActiveCell vs Selection, and When It Breaks)

ActiveCell is a live pointer to the one cell that has the cursor right now — always exactly one cell, on the active sheet, sitting inside the current Selection. Learn how it differs from Selection, how to read and write it with .Value and .Offset, and the number-one reason it breaks — it follows whatever sheet and cursor the user left behind, so it is the wrong tool for code that is not about where the user is.

Henry
VBA Selection in Excel — Work With What's Highlighted (and Why It Isn't Always a Range)

VBA Selection in Excel — Work With What's Highlighted (and Why It Isn't Always a Range)

Selection is a live pointer to whatever is highlighted right now — usually a range of cells, but it can also be a chart, a shape, or nothing at all. That is why code that assumes Selection is a range crashes the moment a chart is selected. Learn how to loop the selected cells, handle multi-area selections with .Areas, guard with TypeName, and when to skip Selection entirely and name your range.

Henry
VBA Select vs Activate in Excel — Break the Macro Recorder's .Select Habit

VBA Select vs Activate in Excel — Break the Macro Recorder's .Select Habit

The macro recorder writes what your mouse does — Select a sheet, Select a cell, act on the Selection — because that is how a person works, not how code should. Learn the real difference between Select (highlight one or many cells) and Activate (set the one active cell), why almost every .Select is a slow, fragile detour you can delete, the rare times you genuinely need to select, and how to refactor recorder code to act on qualified range references directly.

Henry
VBA Worksheet_Change in Excel — Run Code When a Cell Is Edited (and the Infinite Loop to Avoid)

VBA Worksheet_Change in Excel — Run Code When a Cell Is Edited (and the Infinite Loop to Avoid)

Worksheet_Change is the event Excel fires every time a user edits a cell on the sheet, handing you the changed cell as Target. The trap that catches everyone — your handler writes a cell, that write fires the event again, and Excel loops forever. Learn the Application.EnableEvents fix, how to scope it with Intersect, why it ignores formula recalcs, and where the code must live.

Henry
VBA Worksheet_SelectionChange in Excel — Run Code When the Cursor Moves (Highlight the Active Row Without Lag)

VBA Worksheet_SelectionChange in Excel — Run Code When the Cursor Moves (Highlight the Active Row Without Lag)

Worksheet_SelectionChange is the event Excel fires every time the cursor moves — a click, an arrow key, an Enter. It hands you the new selection as Target, which makes cursor-following tricks like highlighting the active row possible. But it fires constantly, so heavy code makes the whole sheet lag. Learn the highlight-active-row pattern done right, why it can loop, and how to keep it featherlight.

Henry
VBA Collection in Excel — The Ordered, Growable List (and Why It Isn't a Dictionary)

VBA Collection in Excel — The Ordered, Growable List (and Why It Isn't a Dictionary)

A VBA Collection is an ordered list that grows as you Add to it — no ReDim, no size guessing. But it has four sharp edges beginners hit every time — it is 1-based not 0-based, you cannot overwrite an item (only Add/Remove), duplicate keys throw error 457, and it has no built-in Exists check. Learn when a Collection beats an Array or a Dictionary, and when it quietly costs you.

Henry