0

如何从将贯穿工作簿的宏中排除这些工作表?无法完成代码,因为我不熟悉排除工作表。

到目前为止我所拥有的:

Dim sh As Worksheet
   If sh.Name <> "Apples" And sh.Name <> "Oranges" And ws.Name <> "Grapes" Then
4

1 回答 1

0

首先,创建一个表示要排除的所有工作表名称的变量:

Const excludeSheets as String = "Apples,Oranges,Grapes,David,Hector,Sheet1"

然后,您可以执行以下操作:

If IsError(Application.Match(sh.Name, Split(excludeSheets,","))) Then
    'code will manipulate the sheets which are not found in the array
    MsgBox sh.Name & " is not excluded!"
Else:
    Msgbox sh.Name & " is excluded!"
End If

更新

预料到您的新代码,我的猜测是您没有分配sh变量。

您可以在循环结构中执行此操作:

Sub ShowMeTheSheets()

    Dim sh as Worksheet
    Const excludeSheets as String = "Apples,Oranges,Grapes,David,Hector,Sheet1"

    For each sh in ThisWorkbook.Worksheets  'Assigns a Worksheet Object to the sh Variable.
        If IsError(Application.Match(sh.Name, Split(excludeSheets,","))) Then
            'code will manipulate the sheets which are not found in the array
            MsgBox sh.Name & " is not excluded!"
        Else:
            Msgbox sh.Name & " is excluded!"
        End If
    Next
End Sub
于 2013-10-09T14:36:23.743 回答