Overview
In this article, I will introduce a minimal Excel VBA code that changes only the outline color of a shape (button) when you click it. This code targets the specific shape you clicked and does not affect other elements like charts or series. You can select the color using the standard Windows “Color” dialog box.
Prerequisites
- You are using Excel for Microsoft 365.
- You know how to place a shape (rectangle, circle, icon, etc.) on a sheet and assign a macro to it.
- The code is designed to work on both 32-bit and 64-bit versions of Excel.
How to Use
- Place a shape on your worksheet.
- Right-click the shape, select “Assign Macro,” and choose
ChangeCallerShapeOutlineColor(code provided below). - When you click the shape, a color selection dialog will open. The outline color of that specific shape will change to the color you select.
Complete Code
Please copy and paste the following code into a standard module. Its function is strictly limited to “changing the outline color of the clicked shape.”
Option Explicit
'==============================
' Color Selection Dialog (32/64-bit Compatible)
'==============================
#If VBA7 Then
Private Type CHOOSECOLOR
lStructSize As Long
hwndOwner As LongPtr
hInstance As LongPtr
rgbResult As Long
lpCustColors As LongPtr
Flags As Long
lCustData As LongPtr
lpfnHook As LongPtr
lpTemplateName As LongPtr
End Type
Private Declare PtrSafe Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" ( _
ByRef pChoosecolor As CHOOSECOLOR) As Long
#Else
Private Type CHOOSECOLOR
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As Long
Flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" ( _
ByRef pChoosecolor As CHOOSECOLOR) As Long
#End If
Private Const CC_RGBINIT As Long = &H1&
Private Const CC_FULLOPEN As Long = &H2&
' Shows the color dialog and returns the selected RGB value. Returns -1 if canceled.
Private Function ShowColorDialog(Optional ByVal DefaultColor As Long = 0) As Long
Dim cc As CHOOSECOLOR
#If VBA7 Then
Dim aCust(0 To 15) As Long ' 16 custom color slots
#Else
Dim CustColors As String
#End If
With cc
.lStructSize = Len(cc)
.hwndOwner = Application.Hwnd
.Flags = CC_RGBINIT Or CC_FULLOPEN
.rgbResult = DefaultColor
#If VBA7 Then
.lpCustColors = VarPtr(aCust(0))
#Else
CustColors = String$(16 * 4, vbNullChar)
.lpCustColors = CustColors
#End If
End With
If ChooseColor(cc) <> 0 Then
ShowColorDialog = cc.rgbResult
Else
ShowColorDialog = -1
End If
End Function
'==============================
' Change only the outline color of the clicked shape (Application.Caller)
'==============================
Public Sub ChangeCallerShapeOutlineColor()
Dim ws As Worksheet
Dim shp As Shape
Dim clr As Long
Set ws = ActiveSheet
' Select a color (exit if canceled)
clr = ShowColorDialog
If clr = -1 Then Exit Sub
' Assumes the caller is a shape and sets the outline color
On Error Resume Next
Set shp = ws.Shapes(Application.Caller)
If Not shp Is Nothing Then
shp.Line.Visible = msoTrue
shp.Line.ForeColor.RGB = clr
End If
On Error GoTo 0
End Sub
Implementation Notes
- Execution Method: This macro assumes it is triggered by clicking a shape. If you run it using a shortcut key or directly from the editor,
Application.Callermay not return the shape name correctly. - Line Visibility: Even if the shape’s outline is currently hidden, this code sets
Visibleto true and applies the color. - Line Style: The thickness and dash style remain unchanged. If necessary, you can add
shp.Line.Weight(thickness) orshp.Line.DashStyle(solid/dotted) to the code to customize them. - Conflicts: If you are using other macros or events on the same sheet, organize your design to ensure multiple operations do not conflict on the same shape.
Summary
I have explained a minimal code setup to change only the outline color of a clicked shape. This is useful for maintaining a consistent look or allowing users to customize colors freely. Feel free to expand the code to change line thickness or fill colors to suit your specific needs.
