Trouble looping through Shapes Collection in Visual Basic

Jaylee52

New Member
Joined
May 21, 2012
Messages
3
Hi Forum,

I've searched the web and tried everything I can think of on my own, but now I am stuck. I have an Excel page with many rounded rectangle shapes on it. There is a subset of them, whose names all start with the same 6 characters, that I want to loop through in order to set them all to the same fill color.

However I am getting a run-time error 13 "Type Mismatch" error with the code below.

Code:
[COLOR="Green"]'Turn all rectangles on the page whose name starts with "RenPan" light teal[/COLOR]
[COLOR="Blue"]Dim[/COLOR] Shp [COLOR="blue"]As Shape[/COLOR]
[COLOR="Blue"]Dim[/COLOR] ShpName [COLOR="blue"]As String[/COLOR]
[COLOR="blue"]For Each[/COLOR] Shp [COLOR="blue"]In[/COLOR] ActiveSheet.Shapes
     ShpName = Left(ActiveSheet.Shapes(Shp).Name, 6)      'Error occurs here
     [COLOR="blue"]If[/COLOR] ShpName = "RelPan" [COLOR="blue"]Then[/COLOR]
          [COLOR="blue"]Call[/COLOR] ButtonLightTeal(ActiveSheet.Shapes(Shp).Name)
     [COLOR="blue"]End If[/COLOR]
[COLOR="Blue"]Next[/COLOR] Shp

Any thoughts? Thanks so much,
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi Forum,

I've searched the web and tried everything I can think of on my own, but now I am stuck. I have an Excel page with many rounded rectangle shapes on it. There is a subset of them, whose names all start with the same 6 characters, that I want to loop through in order to set them all to the same fill color.

However I am getting a run-time error 13 "Type Mismatch" error with the code below.

Code:
[COLOR=green]'Turn all rectangles on the page whose name starts with "RenPan" light teal[/COLOR]
[COLOR=blue]Dim[/COLOR] Shp [COLOR=blue]As Shape[/COLOR]
[COLOR=blue]Dim[/COLOR] ShpName [COLOR=blue]As String[/COLOR]
[COLOR=blue]For Each[/COLOR] Shp [COLOR=blue]In[/COLOR] ActiveSheet.Shapes
     ShpName = Left(ActiveSheet.Shapes(Shp).Name, 6)      'Error occurs here
     [COLOR=blue]If[/COLOR] ShpName = "RelPan" [COLOR=blue]Then[/COLOR]
          [COLOR=blue]Call[/COLOR] ButtonLightTeal(ActiveSheet.Shapes(Shp).Name)
     [COLOR=blue]End If[/COLOR]
[COLOR=blue]Next[/COLOR] Shp

Any thoughts? Thanks so much,
Untested, but I think this code should do what the above code is attempting to do...

Code:
'Turn all rectangles on the page whose name starts with "RenPan" light teal
Dim Shp As Shape
For Each Shp In ActiveSheet.Shapes
  If Shp.Name Like "RelPan*" Then Call ButtonLightTeal(ActiveSheet.Shapes(Shp).Name)
Next Shp
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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