0

我正在跟进我之前的问题,我选择使用一个函数并构建了一个工作表名称列表,这些工作表是正在执行的任务的例外

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

在这种方法中,工作表名称是硬编码的。我也曾一度采取了一种方法来处理例外情况worksheet("blah").index > 2。使用索引号似乎是个坏主意,因为可以移动工作表,这会改变其索引号。在列表中对工作表名称进行硬编码似乎也是一个糟糕的选择,因为可以重命名工作表名称。

有没有办法跟踪工作表的名称或其索引号,以便可以在例外列表中进行维护?

4

1 回答 1

2

您可以一次性查看工作表名称是否在例外列表中。

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
于 2019-03-05T20:55:53.333 回答