Set font color in Excel

with AutoHotkey

by Denis Lamarre
last updated January 31, 2019
difficulty.png easy

by Denis Lamarre

Description

The following AutoHotkey script lets you set a font color in Excel, in this case blue. The script can also be easily adapted to set several other useful commands (like protect/unprotect a worksheet, set a cell formula/color/border, set currency, set conditional formatting, and so on). The script will set the font color if the Excel worksheet is the active window and is not busy (e.g. editing, selecting a command).

Steps

1

overlay-um-excelfontcolor.gifPrepare your overlay
Choose an unassigned key on the Enterpad for your font color function. Name it something appropriate like "Font color BLUE".

2

Code installation
Copy and paste the following script in the AutoHotkey script template file "Enterpad.ahk" at the location of the key you wish to assign.

001:
  IfWinNotActive, ahk_class XLMAIN
  {
    MsgBox % "Excel is not active. No action will be performed."
  Return
  }
  try
    epExcel := ComObjActive("Excel.Application")
  catch
  {
    MsgBox % "Unable to execute the shortcut."
    Return
  }
  try
    epExcel.Selection.Font.ColorIndex := 5
  catch
  {
    MsgBox % "Unable to set the color."
  }
  epExcel :=
return

More Info

The last number (5) at line 15 sets the color of the font to blue. The following table provides an index for some popular colors:

Auto 0
Black 1
White 2
Red 3
Bright Green 4
Blue 5
Yellow 6
Pink 7
Turquoise 8
Dark Red 9
Green 10
Dark Blue 11
Dark Yellow 12
Violet 13
Teal 14
Grey 25 15
Grey 50 16

Some setups allow to run more than one Excel process at the same time. The script above is not intended to work with these kinds of setups. The following post (on the AutoHotkey forum) explains a more efficient way to run a macro while multiple Excel processes are active:

https://autohotkey.com/board/topic/77840-how-to-pass-com-object-pointer-to-a-script/#entry495039