Excel VBA - Color the shapes when shape is selected

jalmbris

New Member
Joined
May 5, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi all ..

I have tried to find answers to this but have not found anything that has solved it for me

sh_name = w.Shapes(Application.Caller).Name <<<< ---- This one gives the error

The object with the specified name could not be found
but it should find the names

VBA Code:
Sub ChangeShapeColor()

Dim sh_name As Variant
Dim w As Worksheet
Dim sp As Shape

With Application
.ScreenUpdating = 0
Set w = Worksheets("Dash")
sh_name = w.Shapes(Application.Caller).Name  ' <<<<< ---- this one gives the error
For Each sp In w.Shapes
w.Shapes.Range(sp.Name).Select
If sp.Name <> sh_name Then
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(248, 248, 248)
With Selection.ShapeRange.TextFrame2.TextRange.Font
    .Fill.ForeColor.RGB = rgbBlack
    .Bold = msoFalse
End With
With Selection.ShapeRange.Line
    .ForeColor.RGB = rgbBlack
    .Weight = 1.2
End With

Else:

Selection.ShapeRange.Fill.ForeColor.RGB = RGB(214, 254, 224)
With Selection.ShapeRange.TextFrame2.TextRange.Font
    .Fill.ForeColor.RGB = vbBlack
'    .Bold = msoCTrue
End With
With Selection.ShapeRange.Line
    .ForeColor.RGB = vbBlack
    .Weight = 1.2
End With

End If
Next sp
[A1].Select
.ScreenUpdating = -1
End With

Blad1.Range("A1:CK200").Interior.Color = RGB(255, 255, 255)

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Have you assigned the above macro to the shape(s) to be changed, it won't work unless this macro is assigned to the shape(s) that is/ are being clicked.

It works on my end with three shapes, it makes the shape being clicked turn green and the other shapes go a light grey colour.
 
Upvote 0
Hi Georgiboy,

when I try to run the code in excel, I get the following error when the code is in Sheet1 "image: Shapes-error.JPG"
and when the code is in Module "siteButtons" I get the following error "image: Shapes-error2.JPG" but the buttons change color but the error message comes up

Regards
jalmbris
 

Attachments

  • Shapes-error.JPG
    Shapes-error.JPG
    134.3 KB · Views: 7
  • Shapes-error2.JPG
    Shapes-error2.JPG
    123 KB · Views: 9
Upvote 0
Hi,

Now I know what the error is due to..
I also have hidden shapes that should be shown when you have clicked on a certain shape,
and it is these hidden shapes that cause errors.

The question is how to change the code so that it also handles these hidden shapes.

Regards
Jalmbris
 
Upvote 0
Maybe swap this bit:
VBA Code:
For Each sp In w.Shapes
w.Shapes.Range(sp.Name).Select
If sp.Name <> sh_name Then
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(248, 248, 248)
With Selection.ShapeRange.TextFrame2.TextRange.Font
    .Fill.ForeColor.RGB = rgbBlack
    .Bold = msoFalse
End With
With Selection.ShapeRange.Line
    .ForeColor.RGB = rgbBlack
    .Weight = 1.2
End With

Else:

Selection.ShapeRange.Fill.ForeColor.RGB = RGB(214, 254, 224)
With Selection.ShapeRange.TextFrame2.TextRange.Font
    .Fill.ForeColor.RGB = vbBlack
'    .Bold = msoCTrue
End With
With Selection.ShapeRange.Line
    .ForeColor.RGB = vbBlack
    .Weight = 1.2
End With

End If
Next sp
For this:
VBA Code:
For Each sp In w.Shapes
    If sp.Visible Then
        w.Shapes.Range(sp.Name).Select
        If sp.Name <> sh_name Then
            Selection.ShapeRange.Fill.ForeColor.RGB = RGB(248, 248, 248)
            With Selection.ShapeRange.TextFrame2.TextRange.Font
                .Fill.ForeColor.RGB = rgbBlack
                .Bold = msoFalse
            End With
            With Selection.ShapeRange.Line
                .ForeColor.RGB = rgbBlack
                .Weight = 1.2
            End With
        Else
            Selection.ShapeRange.Fill.ForeColor.RGB = RGB(214, 254, 224)
            With Selection.ShapeRange.TextFrame2.TextRange.Font
                .Fill.ForeColor.RGB = vbBlack
            '    .Bold = msoCTrue
            End With
            With Selection.ShapeRange.Line
                .ForeColor.RGB = vbBlack
                .Weight = 1.2
            End With
        End If
    End If
Next sp
 
Upvote 0
Solution

Forum statistics

Threads
1,215,168
Messages
6,123,408
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top