繁体   English   中英

宏无法正确保护我的ws

[英]Macro doesn't protect back my ws properly

我在使用宏时遇到了一个小问题,该宏可能会取消保护我的工作表,刷新数据透视表并再次保护ws(有条件)。 它只能部分正常工作,因为它确实保护了ws,它刷新了数据透视表,但是之后发生了奇怪的事情:-它并没有真正恢复行和列的格式设置-它没有正确地保护ws回来(我当您单击“工具,保护”时,ws看起来像是受保护的-但是,您可以取消保护它而无需再次输入密码??!

    Sub RefreshPivotTables()
    ' will remove password and refresh PT
    Dim xpt As PivotTable
        With ActiveSheet
            .Unprotect Password:="milessss"
            For Each xpt In Worksheets("WT-1").PivotTables
                xpt.RefreshTable
            Next xpt
            .Protect Password:="milessss", AllowFormattingCells:=True, _
                AllowFormattingRows:=True, AllowFormattingColumns:=True, _
                AllowUsingPivotTables:=True, EnableOutlining:=True
        End With
    End Sub

见所附图片

有人可以帮忙吗? 干杯-Mile`S

简单的错字错误:

  .Protect AllowFormattingRows:=True
  .Protect AllowFormattingColumns:=True

Rows ,而不是RawsColumns ,而不是Column

来源: Protection.AllowFormattingRows属性(Excel)

错字已得到纠正,但要实现您的目标,您需要使用以下代码:

Private Sub RefreshPivotTables()
' will remove password and refresh PT

Dim xpt As pivotTable
With ActiveSheet
  .Unprotect Password:="milessss"
  For Each xpt In .PivotTables
      xpt.RefreshTable
  Next xpt
  .Protect Password:="milessss", AllowFormattingCells:=True,AllowFormattingRows:=True, AllowFormattingColumns:=True, AllowUsingPivotTables:=True

End With
End Sub

实际上,您通过不同的选项多次使用了保护功能。 因此,每次使用它时,您都会删除以前使用的选项。 因此,在宏末尾,唯一可用的属性是AllowUsingPivotTables但未设置密码。 因此,您需要在单个表达式中设置所有参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM