In this Excel VBA Font Color HEX Tutorial, you learn how to change a font's color with:
- Excel macros;
- The Color property; and
- A HEX (hexadecimal) color.
This Excel VBA Font Color HEX Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process below. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Font Color HEX Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section below.
Table of Contents
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement the contents below.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create:
- Sub procedures: Click here to open.
- Function procedures: Click here to open.
- Refer to objects (click here to open), including:
- Sheets: Click here to open.
- Cells and cell ranges: Click here to open.
- Work with:
- Properties: Click here to open.
- Methods: Click here to open.
- Variables: Click here to open.
- Data types: Click here to open.
- Functions: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- Activate workbook: Click here to open.
- Work with font characteristics: Click here to open.
This Excel VBA Font Color HEX Tutorial is part of a more comprehensive series of Excel VBA Font Color Tutorials.
- Excel VBA Font Color Index in 2 Easy Steps: Click here to open.
- Excel VBA Font Color RGB in 2 Easy Steps: Click here to open.
- Excel VBA Change Font Color Based on Cell Value in 4 Easy Steps: Click here to open.
- Excel VBA Change Font Color for Part of Text in 8 Easy Steps: Click here to open.
- Excel VBA Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA ActiveX Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA UserForm Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA Chart Data Label Font Color in 4 Easy Steps: Click here to open.
- Excel VBA ActiveX Label Font Color in 2 Easy Steps: Click here to open.
- Excel VBA UserForm Label Font Color in 2 Easy Steps: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need consulting services, you may want to consider working with ExcelRescue. ExcelRescue is my usual suggestion for people who (like you) may need help with Excel tasks/projects: Click here to visit ExcelRescue (affiliate link).
The VBA Font Color HEX Snippet Template/Structure
The following is the VBA font color HEX snippet template/structure I explain (step-by-step) in the Sections below.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference.Font.Color = Application.Hex2Dec(Mid(HexColorString, 6, 2) & Mid(HexColorString, 4, 2) & Mid(HexColorString, 2, 2))
The Example Before VBA Font Color HEX
This Excel VBA Font Color HEX Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process below. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Font Color HEX Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section above.
The text in cell A6 of the example worksheet is “Excel VBA Font Color HEX changed by macro”.
This is the cell the VBA font color HEX example macro I create (by following the step-by-step process below) works with. In other words: This is the cell whose font HEX color changes when I execute the example macro.
The following image displays the example worksheet before I execute the VBA font color HEX example macro.
Step 1: Extract the HEX Color's Red, Green, and Blue Components
Extract the 2 digits (RR, GG, BB) representing each of the HEX color's components. HEX colors follow the format “#RRGGBB”, where:
- RR is the color's red component.
- GG is the color's green component.
- BB is the color's blue component.
Work with the Mid function to extract the HEX color's components, as follows:
(1) To extract the 2 digits representing the color's red component (RR), specify Mid's arguments as follows:
- String: A string with the HEX color.
- Start: 2.
- Length: 2.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Mid(HexColorString, 2, 2)
(2) To extract the 2 digits representing the color's green component (GG), specify Mid's arguments as follows:
- String: A string with the HEX color.
- Start: 4.
- Length: 2.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Mid(HexColorString, 4, 2)
(3) To extract the 2 digits representing the color's blue component (BB), specify Mid's arguments as follows:
- String: A string with the HEX color.
- Start: 6.
- Length: 2.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Mid(HexColorString, 6, 2)
Step 1 Example
In the example, I:
- Declare a variable representing the HEX color:
- As of the String data type; and
- With the name “MyHexColor”.
- Work with HEX color “#008037”.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Dim MyHexColor As String
MyHexColor = "#008037"
Therefore, the expressions extracting the 2 digits (RR, GG, BB) representing each of the HEX color's components are as follows:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
'(1) Extract red component of HEX color (RR):
Mid(MyHexColor, 2, 2)
'(2) Extract green component of HEX color (GG):
Mid(MyHexColor, 4, 2)
'(3) Extract blue component of HEX color (BB):
Mid(MyHexColor, 6, 2)
Step 2: Concatenate the HEX Color's Blue, Green, and Red Components
Use the concatenation operator (&) to create a string with the HEX color's components (2 digits each) in the following order:
- Blue (BB).
- Green (GG).
- Red (RR).
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
BB & GG & RR
You extracted these 2 digits (RR, GG, BB) in step #1. Therefore:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Mid(HexColorString, 6, 2) & Mid(HexColorString, 4, 2) & Mid(HexColorString, 2, 2)
Step 2 Example
The MyHexColor variable represents the HEX color.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Mid(MyHexColor, 6, 2) & Mid(MyHexColor, 4, 2) & Mid(MyHexColor, 2, 2)
Step 3: Use the Hex2Dec Worksheet Function to Convert the BGR HEX Color to a Decimal Number
The WorksheetFunction.Hex2Dec method:
- Converts a hexadecimal number to decimal.
- Accepts a single parameter: Arg1. Arg1 is the hexadecimal value you want to convert.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Application.Hex2Dec(Arg1)
Set the Arg1 parameter of the WorksheetFunction.Hex2Dec method to the string you created in step #2. Therefore:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Application.Hex2Dec(Mid(HexColorString, 6, 2) & Mid(HexColorString, 4, 2) & Mid(HexColorString, 2, 2))
Step 3 Example
The MyHexColor variable represents the HEX color.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
Application.Hex2Dec(Mid(MyHexColor, 6, 2) & Mid(MyHexColor, 4, 2) & Mid(MyHexColor, 2, 2))
Step 4: Refer to the Cell Range
Refer to the cell range whose font color you want to change.
In other words: Create a VBA expression that returns a Range object representing the cell range whose font color you want to change.
Consider explicitly including the following references to create a fully qualified object reference returning a Range object:
- A reference to the applicable workbook. The following VBA constructs (among others) may return a Workbook object:
- The Application.ThisWorkbook property.
- The Application.Workbooks and Workbooks.Item properties.
- The Application.ActiveWorkbook property.
- A reference to the applicable worksheet. The following VBA constructs (among others) may return a Worksheet object:
- The Workbook.Sheets and Sheets.Item properties.
- The Workbook.Worksheets and Worksheets.Item properties.
- The Application.ActiveSheet property.
- A reference to the applicable cell range. The following VBA constructs (among others) may return a Range object:
- The Worksheet.Range property.
- The Worksheet.Cells and Range.Item properties.
- The Worksheet.Rows property.
- The Worksheet.Columns property.
- The Range.Range property.
- The Range.Cells and Range.Item properties.
- The Range.Rows property.
- The Range.Columns property.
- The Range.EntireRow property.
- The Range.EntireColumn property.
- The Range.Offset property.
- The Range.CurrentRegion property.
- The Range.End property.
- The Range.Resize property.
Use the following template/structure to refer to the cell range whose font color you want to change:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference
Step 4 Example
In the example, I refer to cell A6 in the “Excel VBA Font Color HEX” worksheet in the workbook where the procedure is stored. I work with the following VBA constructs to obtain a Range object representing this cell:
- The Application.ThisWorkbook property: ThisWorkbook.
- The Workbook.Worksheets and Worksheets.Item properties: Worksheets(“Excel VBA Font Color HEX”).
- The Worksheet.Range property: Range(“A6”).
The resulting expression is:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
ThisWorkbook.Worksheets("Excel VBA Font Color HEX").Range("A6")
Step 5: Set the Value of the Font.Color Property
The Font.Color property sets the color of the applicable font. Set the value of the Font.Color property to the value returned by the WorksheetFunction.Hex2Dec method (in step #3).
Do the following to set the value of the Font.Color property to the value returned by the WorksheetFunction.Hex2Dec method:
(1) Start with the Range object reference you created in step #4.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference
(2) Refer to the following:
- The Range.Font property; and
- The Font.Color property.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference.Font.Color
(3) Set the value of the Font.Color property by referring to the value returned by the WorksheetFunction.Hex2Dec method.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference.Font.Color = Application.Hex2Dec(Arg1)
Considering the expression you created in step #3:
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
WorkbookObjectReference.WorksheetObjectReference.RangeObjectReference.Font.Color = Application.Hex2Dec(Mid(HexColorString, 6, 2) & Mid(HexColorString, 4, 2) & Mid(HexColorString, 2, 2))
Step 5 Example
The MyHexColor variable represents the HEX color.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
ThisWorkbook.Worksheets("Excel VBA Font Color HEX").Range("A6").Font.Color = Application.Hex2Dec(Mid(MyHexColor, 6, 2) & Mid(MyHexColor, 4, 2) & Mid(MyHexColor, 2, 2))
The full VBA font color HEX example macro is as follows:
Sub FontColorHex()
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-font-color-hex/
'Declare variable to represent HEX color
Dim MyHexColor As String
'Assign HEX color to the MyHexColor variable
MyHexColor = "#008037"
'Do the following:
'Step 1: Use Mid function to extract the red, green, and blue components of the HEX color
'Step 2: Use the concatenation operator (&) to create a string with the blue, green, and red components (BGR) of the HEX color
'Step 3: Use the Hex2Dec worksheet function to convert the BGR HEX color to a decimal number
'Step 4: Refer to cell A6 in the "Excel VBA Font Color HEX" worksheet in this workbook
'Step 5: Use the value returned by the Hex2Dec worksheet function to set the value of the Font.Color property
ThisWorkbook.Worksheets("Excel VBA Font Color HEX").Range("A6").Font.Color = Application.Hex2Dec(Mid(MyHexColor, 6, 2) & Mid(MyHexColor, 4, 2) & Mid(MyHexColor, 2, 2))
End Sub
The following GIF illustrates the effects of using the macro example.
Download the VBA Font Color HEX Example Workbook
This Excel VBA Font Color HEX Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process above. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Font Color HEX Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section above.
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement the contents above.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create:
- Sub procedures: Click here to open.
- Function procedures: Click here to open.
- Refer to objects (click here to open), including:
- Sheets: Click here to open.
- Cells and cell ranges: Click here to open.
- Work with:
- Properties: Click here to open.
- Methods: Click here to open.
- Variables: Click here to open.
- Data types: Click here to open.
- Functions: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- Activate workbook: Click here to open.
- Work with font characteristics: Click here to open.
This Excel VBA Font Color HEX Tutorial is part of a more comprehensive series of Excel VBA Font Color Tutorials.
- Excel VBA Font Color Index in 2 Easy Steps: Click here to open.
- Excel VBA Font Color RGB in 2 Easy Steps: Click here to open.
- Excel VBA Change Font Color Based on Cell Value in 4 Easy Steps: Click here to open.
- Excel VBA Change Font Color for Part of Text in 8 Easy Steps: Click here to open.
- Excel VBA Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA ActiveX Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA UserForm Text Box Font Color in 2 Easy Steps: Click here to open.
- Excel VBA Chart Data Label Font Color in 4 Easy Steps: Click here to open.
- Excel VBA ActiveX Label Font Color in 2 Easy Steps: Click here to open.
- Excel VBA UserForm Label Font Color in 2 Easy Steps: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need consulting services, you may want to consider working with ExcelRescue. ExcelRescue is my usual suggestion for people who (like you) may need help with Excel tasks/projects: Click here to visit ExcelRescue (affiliate link).