Object selection VBA code amendment needed if shape is not on active sheet

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am using this code to create a shape on a sheet defined here as sh.

Code:
sh.Shapes.AddShape(msoShapeRectangle, 10, 15, 650, 800).Select

There is no problem with the above line for now since I don’t get any errors.

Then I wanted to manipulate the shape so I did:

Code:
Selection.ShapeRange.Line.Visible = msoFalse

And if the sh is not the active sheet, I get an error message object doesn’t support this………….

How do I fix that please?

Because I get same error on this block too when I comment out the previous line:


Code:
With Selection.ShapeRange.Fill


End With
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Why are you using Selection at all? The AddShape method returns a reference to the created Shape, so store that in a variable and use it directly.
 
Upvote 0
Something like:

VBA Code:
Dim theShape as Shape
Set theShape = sh.Shapes.AddShape(msoShapeRectangle, 10, 15, 650, 800)

then use theShape wherever you need it.
 
Upvote 0
Solution
Something like:

VBA Code:
Dim theShape as Shape
Set theShape = sh.Shapes.AddShape(msoShapeRectangle, 10, 15, 650, 800)

then use theShape wherever you need it.
That’s amazing. Problem solved

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,565
Messages
6,125,583
Members
449,237
Latest member
Chase S

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