CodexBloom - Programming Q&A Platform

VBA: How to correctly reference and iterate over shapes in a protected worksheet?

👀 Views: 1 💬 Answers: 1 📅 Created: 2025-06-09
excel vba shapes worksheet

I'm stuck on something that should probably be simple. I'm trying to iterate over all shapes in a protected Excel worksheet using VBA, but I'm working with issues with accessing specific properties of the shapes. When I attempt to access a shape's `Top` property, I get a 'Run-time behavior 1004' indicating that the method is not available due to the worksheet being protected. I have already tried unprotecting the sheet at the start of my code, but it seems that the shapes are still not accessible. Here’s a snippet of what I have: ```vba Sub IterateShapes() Dim ws As Worksheet Dim shp As Shape Set ws = ThisWorkbook.Worksheets("Sheet1") ' Attempting to unprotect the sheet ws.Unprotect Password:="mypassword" ' Iterating through each shape For Each shp In ws.Shapes Debug.Print shp.Name, shp.Top ' This line throws an behavior Next shp ' Reprotect the sheet ws.Protect Password:="mypassword" End Sub ``` The unprotecting part works correctly, but as soon as I try to access `shp.Top`, I hit the behavior. I’ve also tried adding a delay using `Application.Wait`, but that didn’t resolve the scenario either. Is there a specific reason why I'm unable to access certain properties after unprotecting the worksheet? What’s the best approach to handle this situation? I'm using Excel 2016. What am I doing wrong? I'm working on a API that needs to handle this. Thanks in advance! This is part of a larger API I'm building. Thanks in advance!