可以重命名和移动工作表时如何将其添加到例外列表中

时间:2019-03-05 20:31:09

标签: excel vba

我正在跟踪my earlier question,在这里我选择使用一个函数,并建立了一个工作表名称列表,这些表是正在执行的任务的例外情况

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作为例外。使用索引号似乎是个坏主意,因为可以在工作表中移动索引表,从而更改其索引号。硬编码列表中的工作表名称似乎也是一个糟糕的选择,因为可以重命名工作表名称。

是否可以跟踪工作表的名称或其索引号,以便可以将其保留在例外列表中?

1 个答案:

答案 0 :(得分:1)

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

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