logo
search
VBA & Macro Problems

How to Change an Excel Shape's Color When Clicked Using VBA

Huda QurayshiHuda Qurayshi Oct 9, 2026 869 views

Question details

The user wants to use VBA in Excel to highlight a specific shape by changing its color when clicked, while simultaneously resetting all other shapes to their default color.

How to Change an Excel Shape's Color When Clicked Using VBA
Product
Excel
Device & OS
not provided
Scenario
Creating an interactive spreadsheet or dashboard where visual feedback is required to indicate which shape or button has been clicked.
Observed behavior
Requires a VBA macro configuration that dynamically updates the clicked shape's color and resets the others across any active worksheet without hardcoding sheet names.
Before you start

Ensure that the Developer tab is enabled in your Excel ribbon and that your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) to allow VBA code execution.

Solution 1Recommended

Use a Shared VBA Procedure with ActiveSheet.Shapes

Assign a single, shared VBA macro to all your shapes. The script identifies the clicked shape, updates its RGB fill color, and resets the rest across any worksheet.

Instead of writing a separate macro for every single shape, you can utilize a single shared procedure. By leveraging Application.Caller, the code detects exactly which shape was clicked and processes the color changes dynamically.

1
Name your shapes

Click on each shape in your worksheet, go to the Name Box (located to the left of the formula bar), and give them consistent names like Shape1, Shape2, Shape3, and Shape4. Press Enter after typing each name.

2
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications editor. Go to Insert in the top menu and select Module to create a new blank module.

3
Write the shared procedure

Inside the module, create a Sub procedure (e.g., Sub ColorShape). Add a For Each loop to iterate through ActiveSheet.Shapes. Use an If statement checking Application.Caller: if the shape name matches the caller, set its Fill.ForeColor.RGB to RGB(30, 60, 30). For all other shapes, set the RGB to RGB(65, 5, 5).

4
Assign the macro to your shapes

Return to your Excel worksheet. Right-click the first shape and select Assign Macro. Choose the ColorShape macro from the list and click OK. Repeat this step for the remaining shapes.

Use a Shared VBA Procedure with ActiveSheet.Shapes
Worksheet Flexibility: Because the code relies on ActiveSheet.Shapes rather than a specific sheet name, you can copy these shapes to any other worksheet and the macro will continue to work perfectly.
WPS Spreadsheet Macro Support

Build Interactive Dashboards using WPS Spreadsheet

WPS Office provides robust VBA and macro support, allowing you to easily write, edit, and execute automated shape-coloring scripts just like you would in Microsoft Excel.

  1. 1. Open your workbook: Launch WPS Spreadsheet and open your existing macro-enabled workbook or create a new one.
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon to access the built-in VBA editor.
  3. 3. Insert the Shape Macro: Insert a new Module, paste your shared shape-coloring VBA code, and save the script.
  4. 4. Assign and Click: Right-click your customized shapes within the spreadsheet, assign the macro, and click them to see dynamic color changes.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) macro file formats.Built-in Developer tools and Visual Basic Editor for seamless macro execution.Lightweight architecture with a familiar, easy-to-navigate ribbon interface.A highly cost-effective and powerful alternative for advanced spreadsheet automation.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Application.Caller return an error in the VBA editor?

Application.Caller only works when the macro is physically triggered by clicking a shape, button, or form control. If you attempt to run or debug the macro directly from inside the VBA editor, Excel does not know what called it, resulting in a Type Mismatch or Object error.

How do I find the correct RGB code for my desired shape color?

You can find exact RGB values in Excel by selecting a shape, navigating to Shape Format > Shape Fill > More Fill Colors, and looking under the Custom tab. The dialog will display the precise Red, Green, and Blue numeric values.

Can I assign a different highlight color to each specific shape?

Yes. Instead of applying a single highlight color, you can use a 'Select Case' statement inside your VBA loop. You can evaluate the name of the clicked shape (Application.Caller) and assign a unique RGB value to it based on its specific name.

Will resetting the shape colors affect other charts or images on the worksheet?

If you loop through ActiveSheet.Shapes blindly, it might affect charts or buttons. To prevent this, add a condition in your loop to check the shape type (e.g., If shp.Type = msoShapeRectangle) so that only your designated shapes change color.