2013-10-09 29 views
0

如何从通过工作簿运行的宏中排除这些工作表? 无法完成代码,因为我不熟悉排除工作表。排除在特定工作表上工作的宏

我到目前为止有:

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

回答

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 

UPDATE

预计你的新代码,我的猜测是你没有分配sh变量。

您可以在一个循环strucutre做到这一点:

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 
+0

大卫,我得到一个运行时错误 对象variabe或带块变量未设置 – Ship72

+0

在代码中我也不会发生运行时错误已经提供了,除非你的其他代码是错误的。没有看到你的其他代码,我无法帮助你。如果错误在'If IsError'行出现,那么我猜测你没有正确地将一个对象分配给'sh'变量。请修改您的原始问题以包含您当前正在执行的代码,并让我知道哪一行会引发错误。 –