You can highlight a cell in Excel with code using either VBA (Visual Basic for Applications) or conditional formatting rules. For a one-off highlight, a single line of VBA such as Range("A1").Interior.Color = vbYellow does the job. For a highlight that updates automatically as values change, use conditional formatting with a formula rule. The steps below cover both methods, plus how to save the file so the code keeps working.

What You'll Need

  • Excel for Windows or Mac (the steps here are for the desktop app; the browser version does not run VBA).
  • A workbook saved as .xlsm (macro-enabled) if you plan to use VBA. Standard .xlsx files will drop the code when saved.
  • The Developer tab enabled if you are using VBA. If you do not see it, go to File > Options > Customize Ribbon and tick Developer in the right-hand list. On Mac, Excel > Preferences > Ribbon & Toolbar.
  • Macro security set to allow macros. The exact wording differs by version and region, so look for the Trust Center > Macro Settings area and choose the option that enables macros with notification.

Step-by-Step

Method 1: Highlight a cell with a VBA one-liner

  1. Open your workbook and press Alt + F11 (Windows) or Option + F11 (Mac) to open the VBA editor.
  2. In the Project pane on the left, right-click your workbook name and choose Insert > Module. A blank code window appears.
  3. Paste this line into the module:
    Sub HighlightCell()
        Range("A1").Interior.Color = vbYellow
    End Sub
    Change "A1" to the cell you want, and vbYellow to another colour constant such as vbRed, vbGreen or vbCyan.
  4. Press F5 to run it, or close the editor and press Alt + F8, select HighlightCell and click Run. Cell A1 now has a yellow fill.
  5. To highlight a whole range, use Range("A1:C10").Interior.Color = vbYellow. To clear the fill, set .Interior.ColorIndex = xlNone.
  6. Save the file as Excel Macro-Enabled Workbook (*.xlsm) via File > Save As. If you save as .xlsx, Excel warns that the macro will be removed.

Method 2: Highlight automatically with conditional formatting

  1. Select the cells you want to highlight. For example, select A2:A100.
  2. Go to Home > Conditional Formatting > New Rule.
  3. Choose Use a formula to determine which cells to format.
  4. Enter a formula that returns TRUE for the cells you want highlighted. For example, =$B2>100 highlights every row in the selection where B is over 100. The dollar sign locks the column so the rule follows each row.
  5. Click Format, open the Fill tab, pick a colour and click OK, then OK again.
  6. Change a value in column B and the highlight updates immediately. No code needs to run.

Method 3: Trigger a VBA highlight from a cell change

  1. Press Alt + F11, then double-click the worksheet name (not a module) in the Project pane.
  2. From the left dropdown at the top of the code window, choose Worksheet, and from the right dropdown choose Change.
  3. Paste a short routine that colours Target (the cell just edited), for example Target.Interior.Color = vbYellow. Keep it simple; complex code in this event can slow typing.
  4. Close the editor and test by typing in a cell on that sheet.

Common Mistakes

  • Saving as .xlsx. The code disappears on close. Always use .xlsm.
  • Forgetting the dollar sign in conditional formatting. =B2>100 applied to a range shifts the reference for every cell and highlights the wrong rows. Use =$B2>100.
  • Putting a Change event in a standard module. Worksheet events only fire when placed in the sheet's own code module.
  • Using ActiveCell when you mean a fixed cell. The active cell changes as the user clicks, so the highlight lands somewhere unexpected.
  • Expecting VBA to run in Excel for the web. The browser version cannot execute macros; the file opens read-only for code.
  • Leaving macros disabled. If the yellow warning bar appears on open, click Enable Content, or the code will not run.

Tips

  • Use colour constants (vbYellow, vbRed) rather than RGB numbers; they are easier to read and remember.
  • Prefer conditional formatting over VBA when the highlight depends on a value. It survives copy-paste, works in Excel for the web and needs no macro permissions.
  • To highlight only the active cell, use ActiveCell.Interior.Color = vbYellow in a macro assigned to a keyboard shortcut via the Macros dialog's Options button.
  • Clear old fills before applying a new one with Cells.Interior.ColorIndex = xlNone, otherwise colours stack up and look muddy.
  • Name your ranges (Formulas > Define Name) and refer to the name in code, so the macro keeps working if rows move.

Troubleshooting

  • Nothing happens when you run the macro. Check the macro security setting, and confirm the code is in a module inside the right workbook, not in Personal.xlsb by accident.
  • "Macros have been disabled" bar. Click Enable Content. If your organisation blocks macros, you cannot override it; use conditional formatting instead.
  • Compile error: Expected: end of statement. Usually a missing quote or a smart quote pasted from a website. Retype the quotation marks in the VBA editor.
  • Conditional formatting highlights the wrong cells. Select the rule under Home > Conditional Formatting > Manage Rules, click Edit Rule and check whether the formula uses relative or absolute references.
  • Highlight vanished after reopening. The file was saved as .xlsx, or the rule was applied to a different sheet than the one you are viewing.
  • Mac shortcuts differ. On Mac, use Option + F11 for the editor and check Excel > Preferences for macro security; the menu names are close but not identical to Windows.

Frequently Asked Questions

Can I highlight a cell without using macros?

Yes. Conditional formatting does it with no code and no macro permissions, and it works in Excel for the web. Use a formula rule for anything value-driven.

Why does my VBA highlight disappear when I reopen the file?

The workbook was almost certainly saved as .xlsx, which strips macros. Save as .xlsm and enable content on open.

Can I highlight a cell based on text in another cell?

Yes. In a conditional formatting formula rule, reference the other cell with a locked column, for example =$C2="Overdue", applied to the range you want coloured.

Does VBA work in Excel for the web or on mobile?

No. Macros run only in the desktop app on Windows and Mac. The web and mobile versions can display existing fills but cannot execute VBA.

How do I remove a highlight applied by code?

Run a line such as Range("A1").Interior.ColorIndex = xlNone, or select the cells and use Home > Clear > Clear Formats. For conditional formatting, delete the rule under Manage Rules.

Can a macro highlight a cell when its value changes?

Yes, with a Worksheet_Change event placed in the sheet's code module. Keep the routine short so it does not slow down typing.