talkinggoat
New Member
- Joined
- Feb 1, 2022
- Messages
- 33
- Office Version
- 365
- Platform
- Windows
I am trying to loop through an array list of shapes, but I keep getting the error "Subscript out of range" when the function, ShapeExists is encountered. The code runs fine if I execute the ShapeExists function, by itself. What am I doing wrong?
VBA Code:
Sub Workbook_Open()
Debug.Print "Begin Workbook_Open sub."
Dim ws As Worksheet
Dim wsCount As Integer
Dim shapeList As Variant
Dim element As Variant
CheckFirstRun = True
wsCount = Sheets.Count
Debug.Print "Number of Sheets: " & wsCount
shapeList = Array("exportData", "InitializeData")
Debug.Print "Making sure the Initilize and Export buttons are hidden."
For i = 1 To wsCount
For Each element In shapeList
Debug.Print "Working to hide shape " & element
If ShapeExists(element) = True Then
Debug.Print element & " exists. Working to hide."
HideShapes (element)
End If
Next element
'Sheets(i).Shapes("exportData").Visible = False
'Sheets(i).Shapes("InitializeData").Visible = False
Next i
Debug.Print "End Workbook_Open sub."
End Sub
Sub HideShapes(shapeName As Variant)
Sheets(i).Shapes(shapeName).Visible = False
End Sub
Function ShapeExists(shapeName As Variant) As Boolean
Dim sh As Shape
For Each sh In Sheets(i).Shapes
If sh.Name = shapeName Then ShapeExists = True
Debug.Print ShapeExists
Next sh
End Function