0

I am following up my earlier question where I opted to go with a function and built a list of names of worksheets that are exception to the task being performed

Function exception(Sheet_Name As String) As Boolean
    Dim exceptions(3) As String
    Dim Counter_x As Long
        
    exceptions(0) = "MASTER ITEM LIST"
    exceptions(1) = "ITEM LIST"
    exceptions(2) = "Rebar Protperties"
    exceptions(3) = "Rebar Worksheet"
    
    exception = False
        
    For Counter_x = LBound(exceptions) To UBound(exceptions)
        If Sheet_Name = exceptions(Counter_x) Then
            exception = True
        End If
    Next Counter_x
    
End Function

In this approach the worksheet names are hard coded. I also took an approach at one point for an exception being worksheet("blah").index > 2. Using the index number seems like a bad idea as the worksheet can be moved around which would change its index number. Hard coding the worksheet name in the list also seems like a poor choice as the worksheet name could be renamed.

Is there a way to track the name of the sheet or its index number such that it can be maintained in the exceptions list?

Community
  • 1
  • 1
Forward Ed
  • 9,484
  • 3
  • 22
  • 52

1 Answers1

2

You can see if the worksheet name is within the exception list in one pass.

Function exception(Sheet_Name As String) As Boolean
    Dim exceptions(3) As String

    exceptions(0) = "MASTER ITEM LIST"
    exceptions(1) = "ITEM LIST"
    exceptions(2) = "Rebar Protperties"
    exceptions(3) = "Rebar Worksheet"

    exception = iserror(application.match(Sheet_Name , exceptions, 0))

End Function