Loop Through Array of Shape Names

talkinggoat

New Member
Joined
Feb 1, 2022
Messages
33
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
just a guess, make a string of your element
VBA Code:
       If ShapeExists(cstr(element)) = True Then
 
Upvote 0
The issue seems to have been that the two functions couldn't see the for variable "i" I made the variable public by using
VBA Code:
Public i as Integer
.
 
Upvote 0
I would simplify.

Put this in a regular module

VBA Code:
Sub HideShapes(ShapeList As String) 'ShapeList is a comma separated text string with names of the shapes to hide
    Dim ws, sh, ShapeListCount As Long, ct As Long
    ShapeListCount = Len(ShapeList) - Len(Replace(ShapeList, ",", "")) + 1
    For Each ws In Worksheets
        For Each sh In ws.Shapes
            If InStr(1, ShapeList, sh.Name) Then
                ct = ct + 1
                sh.Visible = False
                If ct = ShapeListCount Then Exit Sub
            End If
        Next
    Next
End Sub

and put this in the ThisWorkbook module:

Power Query:
Sub Workbook_Open()
    HideShapes ("exportData,InitializeData")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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