Friday, March 21, 2014

How to change a shape color in Excel using a VBA macro

Change Excel shape interior color and border color using VBA ColorIndex


VBA:

Sub change_shape_color()
     Dim shp As Shape
     With ActiveSheet.Shapes("My_shape").OLEFormat.Object
         Select Case .Interior.ColorIndex
             Case 3
                 .Interior.ColorIndex = 43 'light green
                 .Border.ColorIndex = 10 'dark green
             Case 43
                 .Interior.ColorIndex = 45 'light orange
                 .Border.ColorIndex = 46 'dark orange
             Case Else
                 .Interior.ColorIndex = 3 'light red
                 .Border.ColorIndex = 30 'dark red
         End Select
     End With
 End Sub


Excel:

Assign macro to Excel shape: Right click > Assign_Macro... and select the macro "change_shape_color" written above.



No comments:

Post a Comment