×

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

By Chris Newman •  Updated: 10/08/17 •  7 min read
How to add custom colors to recent colors section of your Excel Color 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 an RGB color code to get a specific color into the Recent Colors line-up.

Adding RGB colors to recent colors in Excel color Palette

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 you that using keyboard shortcuts 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 of 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

Why is the Above VBA Not Working Properly?

You may be trying the above code and are experiencing some issues such as cells containing the SendKeys values or cell fill colors being changed. This is due to how the SendKeys() function might be running on your particular computer. To account for the performance among various PCs, we can utilize a Windows API function called Sleep that will let us pause the code temporarily while the SendKey function finishes up its action.

Below is an example of how this might look by delaying each part of the keyboard shortcut by half a second. You are more than welcome to play with the pause time frame to try and speed up or slow down the code to your liking. 

VBA Code:

'Declare Sleep() API
  #If VBA7 Then ' Excel 2010 or later
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
  #Else ' Excel 2007 or earlier
    Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
  #End If

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 "%h"
    Sleep 500 'Pause half-second (units in milliseconds)
    SendKeys "h"
    Sleep 500 'Pause half-second (units in milliseconds)
    SendKeys "m"
    Sleep 500 'Pause half-second (units in milliseconds)
    SendKeys "~"
    Sleep 500 'Pause half-second (units in milliseconds)
    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

Using VBA Code Found On The Internet

Now that you’ve found some VBA code that could potentially solve your Excel automation problem, what do you do with it? If you don’t necessarily want to learn how to code VBA and are just looking for the fastest way to implement this code into your spreadsheet, I wrote an article (with video) that explains how to get the VBA code you’ve found running on your spreadsheet.

Getting Started Automating Excel

Are you new to VBA and not sure where to begin? Check out my quickstart guide to learning VBA. This article won’t overwhelm you with fancy coding jargon, as it provides you with a simplistic and straightforward approach to the basic things I wish I knew when trying to teach myself how to automate tasks in Excel with VBA Macros.

Also, if you haven’t checked out Excel’s latest automation feature called Power Query, I have put together a beginner’s guide for automating with Excel’s Power Query feature as well! This little-known built-in Excel feature allows you to merge and clean data automatically with little to no coding!

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 get 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 that 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!

Keep Learning

Chris Newman

Chris Newman

Chris is a finance professional and Excel MVP recognized by Microsoft since 2016. With his expertise, he founded TheSpreadsheetGuru blog to help fellow Excel users, where he shares his vast creative solutions & expertise. In addition, he has developed over 7 widely-used Excel Add-ins that have been embraced by individuals and companies worldwide.