How to Change Excel Shape Fill Color with VBA
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.

- 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.
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.
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.
Press 'Alt + F11' on your keyboard, or go to the Developer tab and click on 'Visual Basic' to open the VBA Editor.
In the Project Explorer pane on the left, right-click your workbook name, select 'Insert', and then choose 'Module'.
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.
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.

Set Shape Fill Color Using a Specific RGB Value
Apply a hardcoded RGB color to a shape's fill instead of extracting it from a worksheet cell.
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. Download and Open WPS Office: Install WPS Office and launch the WPS Spreadsheet application.
- 2. Enable the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to open the macro environment.
- 3. Create Your Macro: Insert a new Module and paste your shape formatting VBA code.
- 4. Run and Automate: Run the macro to instantly update your shape's fill and text colors based on your cell references.

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.




