2013-05-02 50 views
1

上的保護我試圖在打開保護的多個工作表上進行分組/勾畫。 出於某種原因Excel沒有簡單的選項框protecting-所以我用這個宏的代碼時,要做到這一點:Excel:允許在多個工作表上分組/勾畫

Sub group() 
ActiveSheet.EnableOutlining = True' 
ActiveSheet.Protect Contents:=True, UserInterfaceOnly:=True 
End Sub 

我將它設置爲自動運行宏打開工作簿時。我遇到的問題是我希望它適用於所有工作表,而不僅僅是活動工作表。上面的代碼適用於活動工作表,但我仍然需要在其他工作表上手動運行宏以允許大綱工作。

我還需要一些靈活性,因爲有時工作表將被添加或刪除,並且我希望代碼是靈活的,這樣它就會始終影響所有工作表,而無需命名代碼中的每個工作表。

這可能嗎?

謝謝。

回答

0

我想這是你想要的東西:

Option Explicit 
Sub group() 
    Dim ws As Worksheet 
    For Each ws In ThisWorkbook.Worksheets 
    With ws 
     .EnableOutlining = True ' 
     .Protect Contents:=True, UserInterfaceOnly:=True 
    End With 
End Sub 
2

的可能應該是:

Option Explicit 
Sub group() 
Dim ws As Worksheet 

For Each ws In ThisWorkbook.Worksheets 
    With ws 
     .Protect Contents:=True, UserInterfaceOnly:=True 
     .EnableOutlining = True 'add after adding protection to the sheet 
    End With 
Next ws 'you need the next rule for the "For" routine. 
End Sub 

問候保羅