VBA Code to Highlight Selected Cell in Excel
In this article
To highlight the selected cell in Excel, you must use a VBA macro within the Worksheet_SelectionChange event. Because Excel does not have a built-in "follow the cursor" highlight feature, this script triggers every time you move your selection, clearing previous colors and applying a new fill color to the active cell. This is particularly useful for auditing large datasets where it is easy to lose track of which row or column you are currently editing.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A workbook where you have permission to enable macros.
- Basic access to the Visual Basic for Applications (VBA) editor.
- A clear understanding of your existing cell colors, as this basic method will overwrite existing fill colors.
Step-by-Step
- Open your Excel Workbook: Launch the specific file where you want the highlighting effect to occur. Note that VBA code is stored within the workbook, so if you want this in every file, you would need to save it in your Personal Macro Workbook.
- Enter the VBA Editor: Press
Alt + F11on your keyboard. This is the universal shortcut to open the Visual Basic Editor window. If you are on a laptop, you may need to pressFn + Alt + F11. - Locate the Correct Sheet: On the left-hand side of the screen, you will see the Project Explorer pane. Look for the folder labeled
Microsoft Excel Objects. You will see a list of all sheets in your current workbook (e.g., Sheet1, Sheet2). Double-click the specific sheet where you want the highlight to function. This opens the code window specifically for that worksheet's events. - Select the SelectionChange Event: At the top of the code window, you will see two dropdown menus. The left dropdown says
(General). Click it and selectWorksheet. Once you do this, the right dropdown will automatically populate withSelectionChange. This creates the structural shell of the macro:Private Sub Worksheet_SelectionChange(ByVal Target As Range)andEnd Sub. - Insert the Highlight Code: Between those two lines, type or paste the following code:
Cells.Interior.ColorIndex = 0 Target.Interior.ColorIndex = 6
In this example,Cells.Interior.ColorIndex = 0removes all existing fill colors from every cell in the sheet, andTarget.Interior.ColorIndex = 6applies a bright yellow color to the cell you just clicked. - Customize the Color (Optional): If you do not want yellow, change the number 6 to another index. For example, 3 is red, 4 is bright green, and 8 is light blue. If you prefer a specific RGB color, you can use
Target.Interior.Color = RGB(255, 200, 0). - Save as Macro-Enabled Workbook: Close the VBA window by clicking the X in the top right. Go back to Excel. You must save your file as an
Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx file, the code will be deleted and the highlighting will stop working the next time you open the file. - Test the Function: Click on any cell in the worksheet. The cell should immediately turn yellow, and as you move to another cell, the previous highlight should disappear and move to the new selection.
Common Mistakes
The most frequent error is placing the code in a standard Module instead of the Sheet Object. If you go to Insert > Module and paste the code there, it will not run automatically because the Worksheet_SelectionChange event only triggers when it is placed inside the specific sheet's code window. A standard module requires you to run the macro manually via a button or the Alt+F8 menu, which defeats the purpose of a dynamic highlight.
Another common mistake is forgetting to save the file in the .xlsm format. Many users spend time perfecting their code only to find it gone after a restart because they saved it as a standard workbook. Always check the file extension in the title bar.
Users also frequently overlook the impact on existing formatting. The command Cells.Interior.ColorIndex = 0 is a "nuclear option"; it wipes every single fill color from the entire worksheet. If you have a header row colored blue or specific cells highlighted for errors, this code will erase those colors the moment you click a cell. To avoid this, advanced users often use Conditional Formatting combined with VBA, rather than direct Interior Color changes.
Tips
To highlight an entire row instead of just a single cell, change Target.Interior.ColorIndex = 6 to Target.EntireRow.Interior.ColorIndex = 6. This creates a "crosshair" effect that makes reading data across a wide table significantly easier, as your eyes won't drift to the wrong row.
If you want to highlight both the row and the column, you can add a second line: Target.EntireColumn.Interior.ColorIndex = 6. This creates a intersecting highlight at the active cell, which is the gold standard for data entry and auditing tasks.
To prevent the code from wiping out your existing colors, you can use a named range or a specific area. Instead of Cells.Interior.ColorIndex = 0, you can specify a range like Range("A1:Z100").Interior.ColorIndex = 0. This ensures that any formatting outside that specific box remains untouched.
For a more professional look, avoid the bright primary colors of the ColorIndex list. Use the RGB function to create soft pastels. For example, RGB(230, 230, 250) creates a light lavender that is visible but not distracting, allowing you to still see the black text clearly.
Troubleshooting
If the highlighting is not working, the first thing to check is whether macros are enabled. Look for a yellow "Security Warning" bar at the top of the spreadsheet that says "Macros have been disabled." Click Enable Content to allow the VBA code to run. If the bar doesn't appear, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure that "Disable all macros with notification" is selected.
If the code is running but you are getting a "Run-time error," check for protected sheets. If the worksheet is protected (Review > Protect Sheet), VBA cannot change the cell colors. You must either unprotect the sheet or add a line of code at the beginning of the macro to unprotect it using a password, and another line at the end to protect it again.
If the highlight is "stuck" and won't move, check if you have other macros running that might be interfering with the selection event. Additionally, ensure that Application.EnableEvents has not been set to False by another script. If it has, the SelectionChange event will never trigger. You can fix this by opening the Immediate Window in the VBA editor (Ctrl + G), typing Application.EnableEvents = True, and pressing Enter.
Frequently Asked Questions
Will this work on Excel for the Web?
No, VBA macros do not run in the browser-based version of Excel. This functionality requires the desktop application for Windows or macOS.
Does this slow down my workbook?
In very large workbooks with thousands of formulas, this can cause a slight lag because Excel must recalculate and redraw the screen every time you change your selection. For most standard sheets, the impact is negligible.
Can I use this to highlight cells based on their value?
While you can do this with VBA, Conditional Formatting is a much better tool for value-based highlighting. This specific VBA code is designed for the active selection, regardless of what is inside the cell.
Can I apply this to all sheets at once?
To apply this to every sheet, you must place the code in the ThisWorkbook object instead of a specific sheet object, and use the Workbook_SheetSelectionChange event instead of the Worksheet_SelectionChange event.
Will this remove my borders?
No, the Interior.ColorIndex property only affects the fill color (the background). It does not touch the borders, font colors, or number formatting of the cells.
How do I stop the highlighting?
To stop the effect, you can either delete the code from the VBA editor or comment it out by adding an apostrophe (') at the start of each line of code. Remember to manually clear the last highlight color from your sheet, as the code won't run to clear it once deleted.