To highlight the active cell in Excel for Microsoft 365 on Windows, put a Worksheet_SelectionChange event in that worksheet’s code module and apply a temporary fill to the selected cell. You do not need a VBA array for this: the event responds whenever the selection changes, while an array is useful for processing values rather than drawing cell formatting.
What “active cell” means in Excel
The active cell is the single cell shown as the current cell within a selection. If you select a range such as B2:D5, the range is selected but only one cell is active; Excel marks it with the active-cell outline. A VBA selection-change event receives the selected range as its Target argument, which may contain one cell or many.
For a simple highlight, the macro can color the cell whenever the user moves the selection. The example below handles single-cell selections and restores the previous cell’s fill before applying the new one. It remembers common fill properties, but it is not a perfect preservation method for every specialized format, such as theme-based or gradient fills. If the workbook relies on complex cell formatting, consider the alternatives described below or test on a copy first.
VBA code to highlight the selected cell
This version colors the active cell pale yellow and restores the previous cell’s ordinary fill when the selection moves. It deliberately does not use ActiveCell as the event input; using Target makes the event’s behavior clearer and lets it check whether the selection contains exactly one cell.
Private mPreviousCell As Range
Private mOldPattern As Long
Private mOldColor As Long
Private mOldPatternColor As Long
Private mOldTint As Double
Private mOldPatternTint As Double
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Restore the fill saved for the previously selected cell.
If Not mPreviousCell Is Nothing Then
With mPreviousCell.Interior
.Pattern = mOldPattern
If mOldPattern <> xlNone Then
.Color = mOldColor
.PatternColor = mOldPatternColor
.TintAndShade = mOldTint
.PatternTintAndShade = mOldPatternTint
End If
End With
End If
Set mPreviousCell = Nothing
' Skip multi-cell selections rather than coloring an arbitrary cell.
If Target.Cells.CountLarge <> 1 Then Exit Sub
Set mPreviousCell = Target
With Target.Interior
' Save the existing fill properties before changing the color.
mOldPattern = .Pattern
mOldColor = .Color
mOldPatternColor = .PatternColor
mOldTint = .TintAndShade
mOldPatternTint = .PatternTintAndShade
.Color = RGB(255, 255, 153)
End With
End Sub
The code applies to the worksheet whose code module contains it. Its stored previous-cell information is held in memory while the VBA project is running; it is not a permanent record of the original formatting. The fill color is specified by RGB(255, 255, 153). Change those three numbers to choose another RGB color, or replace the assignment with a different fill approach.
Where to put the event procedure
- Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z.
- Open the Visual Basic Editor. On Windows, press Alt+F11, or use Developer > Visual Basic. If Developer is not visible, enable the Developer tab in Excel options.
- In the Project Explorer, find the workbook, expand Microsoft Excel Objects, and double-click the specific worksheet you want to use.
- Paste the procedure into that worksheet’s code window, not into a standard module. The
Worksheet_SelectionChangeevent is recognized in the sheet module. - Save the file as an Excel Macro-Enabled Workbook (
.xlsm) and enable macros when opening it, if your organization’s security settings permit.
Return to that worksheet and select different cells to see the effect. The event fires when the selection changes, not when you merely edit the contents of the already selected cell. If macros are disabled, or the workbook is saved as a macro-free .xlsx file, the VBA behavior will not run or be retained.
How the event and fill restoration work
Excel calls Worksheet_SelectionChange after a user changes the selection on the sheet. Target represents the newly selected range. The procedure first restores the saved fill on the previous cell, clears its stored reference, and then checks whether the new target contains one cell. If it does, the procedure saves that cell’s fill properties and applies the highlight.
The CountLarge check avoids treating a multi-cell selection as if it were a single cell. With this example, selecting a range removes the last single-cell highlight but does not add a new one. That is a deliberate, predictable choice. If you would rather highlight the active cell even when a range is selected, change the logic to work with ActiveCell after restoring the old fill, and save the active cell’s fill in the same way. Test that variation with keyboard range selection and mouse selection.
Fill formatting is separate from cell values, formulas, and number formats. The procedure does not change those contents. It does, however, temporarily change the cell’s fill, so exact preservation matters if the worksheet uses carefully designed formatting. The example saves several common interior properties; still, rewriting color properties can alter how a theme color or a more elaborate fill is represented. For a sheet with intricate styling, test on a duplicate and check the cell after moving away from it.
What arrays have to do with highlighting
A VBA array stores a group of values in memory. It can be useful when a macro reads thousands of cells, evaluates their values, and writes results back efficiently. An array does not itself highlight a cell; formatting is a property of a worksheet Range. For one active cell, the event-based range approach is simpler than loading worksheet values into an array.
Arrays may make sense in a different task—for example, evaluating many cells and then applying formatting to the cells that meet a rule. Even then, the usual design is to read values into an array, decide which cells qualify, and use worksheet range formatting for the result. Do not confuse that processing pattern with an “array formula” or a dynamic-array formula: neither automatically gives a cell a visual highlight based on the current selection.
Common problems and their fixes
- Nothing happens: Confirm the code is in the intended worksheet module, not Module1, and that macros are enabled. Also check that you are changing the selection on that same sheet.
- The original fill looks different afterward: The sample restores common fill properties but may not reproduce every theme or gradient format exactly. Test on a copy; for complex formatting, use conditional formatting or a carefully designed helper mechanism instead.
- The highlight remains after closing or switching sheets: The example tracks the previous cell only within that worksheet’s running VBA state. Switching away does not necessarily trigger that sheet’s selection-change event, so the last highlight can remain visible there. A workbook-wide implementation needs additional sheet activation/deactivation handling.
- A selection of several cells is not highlighted: That is the stated behavior of the
CountLargecheck. Decide whether you want no highlight for multi-cell selections or want the event to highlight the active cell within the selected range. - Macros are blocked: Excel for the web cannot run VBA macros. On Windows desktop, workbook security settings and organizational policy can also prevent macros from running; do not lower security settings for files you do not trust.
Highlighting only the active row or column
Some users want a crosshair effect: highlight the active cell’s entire row, column, or both. That is a different requirement from highlighting one cell. A selection-change macro can apply formatting to the relevant row or column, but it must also restore the previous row or column without erasing existing fills. Since whole rows and columns can contain intentional formatting, a naive approach that clears their fills can damage a worksheet’s appearance.
For a large table, consider a conditional-formatting design or a dedicated visual indicator rather than changing every cell’s fill on each selection. Whichever approach you choose, define the area it affects—for example, only the used table range rather than all 1,048,576 rows—and test what happens when the selection moves into blank cells. This keeps the effect responsive and limits unintended formatting changes.
VBA versus conditional formatting
VBA is suitable when the highlight must follow the current selection immediately and you are comfortable distributing a macro-enabled workbook. Its main trade-offs are macro security, the need to maintain event code, and the risk of changing existing formatting. Users without macros enabled will not see the event-based effect.
Conditional formatting is better for highlighting cells based on their contents—for example, overdue dates, duplicate values, or values above a threshold. It does not inherently mean “format whichever cell the user has just selected”; selection is not a normal cell value that a standard rule tracks reliably on every selection change. If the goal is simply to identify the active cell while navigating, the event procedure is the direct VBA tool. If the goal is to flag data, use conditional formatting rules designed around that data instead.
Windows, Mac, and web differences
The worksheet event concept and the code are for desktop Excel with VBA support. On Windows, Alt+F11 opens the Visual Basic Editor in the usual configuration; Developer > Visual Basic is another route. On a Mac, VBA is available in desktop Excel, but keyboard shortcuts can vary with the keyboard and macOS settings; use the Developer tab’s Visual Basic command if Option+F11 does not open the editor. The workbook must still be saved in a macro-enabled format.
Excel for the web can open and edit many workbook types, but it cannot create, edit, or run VBA macros. The macro will not respond to selection changes in the browser. If people need the workbook to work in Excel for the web, choose a non-VBA approach suited to the actual task, and verify that it provides the required behavior rather than assuming a desktop event will carry over.
Frequently Asked Questions
Can I highlight the active cell without VBA?
For a highlight that follows selection exactly, VBA is a common approach, but Excel does not provide a simple built-in toggle that permanently tracks the active cell’s fill. Conditional formatting is excellent for data-based rules, but it is not a dependable selection-change event substitute.
Why does the code go in a worksheet module?Worksheet_SelectionChange is a worksheet event. Excel connects it to the sheet whose code module contains the procedure. A standard module can hold ordinary macros, but placing this event procedure there will not make it respond to that sheet’s selection changes.
Can the macro highlight a multi-cell selection?
Yes, but decide whether to color every cell in the selection or only its active cell. The example skips multi-cell selections to avoid an ambiguous result. Coloring the whole range requires a restoration strategy for every affected cell, especially if those cells already have distinct fills.
Will the highlight change my cell’s data?
No. The sample changes the cell’s interior formatting, not its value or formula. It can affect the appearance and representation of the original fill, so preserve a backup and test the code if the sheet uses specialized formatting.
Can I undo the highlight with Ctrl+Z?
No. Excel’s undo stack does not undo VBA macro actions in the ordinary way, and running a macro can clear the available undo history. Save a copy before adding or running code, and remove the event procedure or restore a backup if necessary.
Related
- Excel Power Query: Open It, Import Data, and Refresh
- Excel Notched Box Plot: What It Shows and How to Make One
- Excel Online Page Break Preview: Limits and Workarounds
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Active Workbook: Meaning and ExamplesIn Excel VBA, ActiveWorkbook refers to the workbook currently active in Excel’s window. Use it when a macro…
- Excel VBA UserForm ListBox: Sorting Options and CodeTo sort a list in an Excel VBA UserForm, sort the values before adding them to the ListBox;…
- VBA Sort Order in Excel: Rules and ExamplesIn Excel VBA, sort order is set with xlAscending or xlDescending; the sort key determines which column or…
- VBA Macro to Copy and Paste a Row Below the Last RowTo copy the last data row into the row immediately below it, find the last occupied cell in…
- VBA MsgBox Location: Where It Appears and How to Control ItA VBA MsgBox has no built-in argument for setting its screen coordinates. In Excel for Microsoft 365 on…
- Excel VBA UserForm Image: Add, Load, and TroubleshootAn Excel VBA UserForm displays a picture with the Microsoft Forms Image control: add the control in the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.