vba Excel:允许在多个工作表上分组/大纲,并保护

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16342811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:56:30  来源:igfitidea点击:

Excel: Allowing grouping/outlining on multiple worksheets with protection on

excel-vbagroupingprotectionoutliningvba

提问by user2343885

I am trying to allow grouping/outlining on multiple worksheets that have protection turned on. For some reason excel doesn't have a simple option box to do this when protecting- so I'm using this macro code:

我正在尝试允许在打开保护的多个工作表上进行分组/概述。出于某种原因,excel 在保护时没有一个简单的选项框来执行此操作 - 所以我使用了这个宏代码:

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

I set it up as an auto-run macro when the workbook is opened. The issue I have is that I want it to apply to all sheets, not just the active sheet. The code aboves works on the active sheet, but I still have to manually run the macro on the other sheets to allow the outlining to work.

当工作簿打开时,我将其设置为自动运行宏。我的问题是我希望它适用于所有工作表,而不仅仅是活动工作表。上面的代码适用于活动工作表,但我仍然必须在其他工作表上手动运行宏以允许大纲工作。

I also need some flexibility in that sometimes worksheets will be added or deleted, and I want the code to be flexible so that it always affects all worksheets without me having to name every worksheet in the code.

我还需要一些灵活性,因为有时会添加或删除工作表,并且我希望代码具有灵活性,以便它始终影响所有工作表,而不必在代码中命名每个工作表。

Is this possible?

这可能吗?

Thank you.

谢谢你。

回答by Paul

The could should be:

应该是:

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

Greetings Paul

问候保罗

回答by Alistair Weir

I think this is what you want:

我认为这就是你想要的:

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