VBA Selecting Shapes

Jasesair

Active Member
Joined
Apr 8, 2015
Messages
282
Office Version
  1. 2016
I have the below code which is working quite well. I'm wanting it to only be changing the colours from a range of the shapes I have on the sheet, not them all. Any idea how to do this?

VBA Code:
Sub SetColor(v)
  Dim cell As String
  cell = ActiveCell.Address
  ActiveSheet.Shapes.SelectAll
  Selection.ShapeRange.Fill.ForeColor.RGB = RGB(175, 171, 171)
  ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(224, 255, 124)
  Range(cell).Activate
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
When you say a range of shapes, do you shapes that exist in a certain range (eg A1:D100)
or do you mean shapes with certain names?
 
Upvote 0
Unfortunately the shapes are all in a large cell A1, with other shapes also in the cell that I don't want included. I have labelled the shapes but I'm not sure if that's the same as naming them. They're just labelled from 1-10.
 
Upvote 0
OK, try
Rich (BB code):
Sub SetColor(v)
   ActiveSheet.Shapes.Range(Array("Oval 2", "Oval 4", "Oval 3")).Fill.ForeColor.RGB = RGB(175, 171, 171)
   ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(224, 255, 124)
End Sub
Change the values in red to match the names of your shapes. To find the names click on one of the shapes & look in the name box (to the left of the formula bar).
 
Upvote 0
Solution
Hi Fluff, thanks so much for all your help. Not sure what is the problem, but I'm getting an error on the first line. I can't see what would be wrong!

VBA Code:
Sub SetColor(v)
    ActiveSheet.Shapes.Range(Array("Rectangle 26", "Rectangle 51", "Rectangle 52", "Rectangle 53", "Rectangle 54", "Rectangle 55", "Rectangle 56", "Rectangle 57", "Rectangle 58", "Rectangle 59")).Fill.ForeColor.RGB = RGB(175, 171, 171)
    ActiveSheet.Shapes(v).Fill.ForeColor.RGB = RGB(224, 255, 124)
End Sub
 
Upvote 0
What error do you get?
 
Upvote 0
Well, that's all it took - a little prompt to actually look at the error message! Turns out the shapes were all 'rounded rectangles'!

This now works a treat. Many thanks for all your assistance!!!
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

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