2013-10-09 97 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'變量。請修改您的原始問題以包含您當前正在執行的代碼,並讓我知道哪一行會引發錯誤。 –

相關問題