How to convert hex codes to cell colors in Excel with VBA

You will use a short VBA macro to turn a column of hex codes into matching cell fill colors in Excel, so a whole palette appears in seconds instead of hours of manual clicking. It suits anyone comfortable pasting code into the VBA editor. You need desktop Excel and a macro-enabled workbook.

YouTube video: How to Convert Hex Codes to Cell Colors Automatically in Excel

YouTube’s video details do not confirm captions. Check CC in the player for available tracks.

Video length
1 min 5 s
Time to do it
10 minutes
Difficulty
Medium
Steps
6

What you need

Steps

  1. Open your workbook and type each hex code into its own cell in one column, like 1F4E79 or #1F4E79. Save the file as an Excel Macro-Enabled Workbook (.xlsm).

  2. Press Alt+F11 on Windows or Option+F11 on Mac to open the VBA editor. In the menu, click Insert, then Module.

  3. Paste the ApplyHexColors macro into the blank module. It strips the #, splits the code into red, green and blue pairs, then sets cell.Interior.Color.

  4. Close the VBA editor and select the cells holding your hex codes. Only the selected cells get colored.

  5. Press Alt+F8, pick ApplyHexColors and click Run. On Mac, use Tools, Macro, Macros instead.

  6. Check the fills. If nothing changed, make sure macros are allowed in File, Options, Trust Center, and that every code has exactly 6 characters.

Tips and common mistakes

Save a backup copy before running any macro, because Ctrl+Z will not undo the color fills.

Six-digit codes only. Short forms like #FFF need expanding to FFFFFF first.

Keep the On Error Resume Next line so one bad cell is skipped instead of stopping the whole run.

Re-run the macro any time you edit the codes, since the colors do not update by themselves.

Questions

Does this work in Excel for Mac?

Yes, VBA runs in desktop Excel for Mac. Open the editor with Option+F11 and launch the macro from Tools, Macro, Macros.

Can I undo the colors the macro applies?

Not with Ctrl+Z, because macros are not tracked in the undo history. Work on a copy of the file or clear the fill color manually if you need to start over.

Why did some cells stay uncolored?

The macro only fills cells whose value is exactly 6 characters after the # is removed. Check for typos, extra spaces or 3-digit shorthand codes.

The notes are drafted automatically from video details and general task knowledge. How the guide was prepared

Report a correction

More in Money & Office

See all