2013-05-02 54 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 

问候保罗