Excel VBA To Add Custom Colors To Recent Colors Section of Palette

A Little Background

This post shows how you can use VBA code to add specific colors (outside your color theme) to the Recent Colors section of your Excel Color Palette. I have actually been trying to figure this out on and off for years with no success until a week ago when I stumbled on a little shortcut that led me to the VBA solution in this post.

How To Quickly (Manually) Add A Color To Recent Colors

So the tactic I stumbled upon to quickly add a color from your spreadsheet to your Recent Colors section goes as follows:

  1. Select the cell with the desired color (in this example we are targeting the Fill Color)
  2. Go to the More Colors button in the Home tab (keyboard shortcut Alt+H+H+M)
  3. Hit Ok

By doing those three steps, you do not have to manually type in a RGB color code to get a specific color into the Recent Colors line-up.

What This VBA Code Does

With that process in mind, I was able to write the below code that takes a list of RGB color codes (each value 3 characters long) and follow the steps listed previously

  1. Apply a color to a cell
  2. Use keyboard Shortcut Alt+H+H+M to open up the More Colors Dialog
  3. Hit the Enter Key (tilde sign in VBA)

I will warn that using keyboard shortcut via VBA can sometimes not work. I haven't had any problems with this particular script but I have had issues with other code that attempted to programmatically use keyboard shortcuts. I use the DoEvents call a couple times in order to try and mitigate any issues that might arise with the SendKeys function.

VBA Code:

Sub LoadRecentColors()
'PURPOSE: Use A List Of RGB Codes To Load Colors Into Recent Colors Section of Color Palette
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim ColorList As Variant
Dim CurrentFill As Variant

'Array List of RGB Color Codes to Add To Recent Colors Section (Max 10)
  ColorList = Array("066,174,093", "184,055,038", "046,062,081", "056,160,133")

'Store ActiveCell's Fill Color (if applicable)
  If ActiveCell.Interior.ColorIndex <> xlNone Then CurrentFill = ActiveCell.Interior.Color

'Optimize Code
  Application.ScreenUpdating = False

'Loop Through List Of RGB Codes And Add To Recent Colors
  For x = LBound(ColorList) To UBound(ColorList)
    ActiveCell.Interior.Color = RGB(Left(ColorList(x), 3), Mid(ColorList(x), 5, 3), Right(ColorList(x), 3))
    DoEvents
    SendKeys "%hhm~"
    DoEvents
  Next x

'Return ActiveCell Original Fill Color
  If CurrentFill = Empty Then
    ActiveCell.Interior.ColorIndex = xlNone
  Else
    ActiveCell.Interior.Color = currentColor
  End If

End Sub

How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

Chris
Founder, TheSpreadsheetGuru.com