logo
search
VBA & Macro Problems

How to Change Excel Shape Fill Color with VBA

Kushani NimanthikaKushani Nimanthika Oct 23, 2026 868 views

Question details

The user wants to use a VBA macro to change a shape's fill color and text color so that it matches the formatting of a specific worksheet cell.

How to Change an Excel Shape Fill Color with VBA
Product
Excel
Device & OS
not provided
Scenario
Automating shape formatting to dynamically inherit and match worksheet cell background and font colors using VBA scripts.
Observed behavior
The goal is to programmatically apply the .Interior.Color and .Font.Color of a referenced cell to a target shape's .Fill.ForeColor.RGB and text color attributes.
Before you start

Ensure that the Developer tab is enabled in your spreadsheet ribbon and that your document is saved as a Macro-Enabled Workbook (.xlsm) to successfully execute VBA code.

Solution 1Recommended

Match Shape Fill and Text Color to a Cell Using VBA

Use a simple VBA macro to dynamically extract the background and font colors from a specific cell and apply them to a shape.

This method utilizes the shape's `.Fill.ForeColor.RGB` property and maps it directly to a cell's `.Interior.Color`. It also matches the text color using the cell's `.Font.Color`, ensuring your shape looks identical to your target cell.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard, or go to the Developer tab and click on 'Visual Basic' to open the VBA Editor.

2
Insert a New Module

In the Project Explorer pane on the left, right-click your workbook name, select 'Insert', and then choose 'Module'.

3
Enter the VBA Code

Paste the following logic into the module window: reference your target shape by name (e.g., ActiveSheet.Shapes("Rectangle 1")), set its .Fill.ForeColor.RGB = Range("A1").Interior.Color, and set its .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = Range("A1").Font.Color.

4
Run the Macro

Close the VBA Editor, return to your worksheet, and press 'Alt + F8'. Select your newly created macro and click 'Run' to apply the formatting to the shape.

Match Shape Fill and Text Color to a Cell Using VBA
Verify Shape Name: Make sure to replace "Rectangle 1" and "A1" in your code with the actual name of your shape and the cell reference you wish to copy formatting from.
Powerful VBA Support

Use WPS Spreadsheet for Seamless VBA and Macros

WPS Office fully supports VBA macros, allowing you to easily automate shape formatting, match cell colors, and streamline your spreadsheet tasks just like in Microsoft Excel.

  1. 1. Download and Open WPS Office: Install WPS Office and launch the WPS Spreadsheet application.
  2. 2. Enable the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to open the macro environment.
  3. 3. Create Your Macro: Insert a new Module and paste your shape formatting VBA code.
  4. 4. Run and Automate: Run the macro to instantly update your shape's fill and text colors based on your cell references.
Fully compatible with Microsoft Excel VBA macros and .xlsm filesFree and lightweight alternative to Microsoft OfficeFamiliar user interface for easy macro management and code editingAdvanced shape and cell formatting capabilities
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA code not changing the shape color?

Ensure you have referenced the correct shape name or index in your code. You can find the exact name of your shape by selecting it and looking at the Name Box located to the left of the formula bar.

Can I change the shape outline or border color with VBA?

Yes, you can modify the shape's outline color by using the `.Line.ForeColor.RGB` property in your VBA script instead of `.Fill.ForeColor.RGB`.

Do I need to save my workbook differently if it contains VBA code?

Yes, to preserve your VBA scripts, you must save your file as an Excel Macro-Enabled Workbook (.xlsm) instead of a standard .xlsx file.

How do I apply the color change to all shapes on the active worksheet?

You can loop through all shapes on the sheet using a 'For Each' loop in VBA. For example: `For Each shp In ActiveSheet.Shapes` followed by the color assignment code.