Excel VBA How to change Shape properties in a different Sheet.

Anonymous
2023-03-13T20:46:03+00:00

I have a workbook where Sheet1 has a number of Shape objects. The Properties of some of these are changed by a sub that is invoked from OnTime. If Sheet1 is the active sheet the sub runs correctly. If another Sheet is active when the Sub executes, my code throws an error. Below is an example of the problem code:

Sheet1.Shapes.Range(Array("Rounded Rectangle 22")).Select

With Selection.ShapeRange.Fill 

    .Visible = msoTrue 

    .ForeColor.RGB = RGB(255, 124, 128) ' red 

    .Transparency = 0 

    .Solid 

End With

The error occurs on the With statement.

I have a temporary workaround whereby I activate Sheet1 at the start of my Sub and then return to the previously active sheet on completion. It works, but I'm not sure that is the best solution.

Is there a way to code this so it will work when a Sheet other than Sheet1 is active?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2023-03-13T21:03:19+00:00

    Select and Selection only work on the active sheet, so you have to avoid selecting cells.

        With Sheet1.Shapes("Rounded Rectangle 22").Fill 
            .Visible = msoTrue 
            .ForeColor.RGB = RGB(255, 124, 128) ' red 
            .Transparency = 0 
            .Solid 
        End With
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-03-13T21:35:34+00:00

    Yes, that works. Thanks!

    0 comments No comments