【问题标题】:Macro doesn't protect back my ws properly宏不能正确保护我的 ws
【发布时间】:2018-09-15 08:06:59
【问题描述】:

我遇到了一个宏的小问题,它假设取消保护我的工作表、刷新数据透视表并再次保护 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

【问题讨论】:

    标签: vba excel excel-2003


    【解决方案1】:

    简单的拼写错误:

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

    Rows 代替 RawsColumns 代替 Column

    来源:Protection.AllowFormattingRows Property (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,但没有设置密码。因此,您需要在一个表达式中设置所有参数。

    【讨论】:

    • 谢谢Jean ...我真的很累,但还是很遗憾我没有看到那些错别字:(现在,我发现了这个宏的真正问题——我已经改变了我的问题正确描述问题 - 宏不能以应有的方式保护我的 ws。这是什么原因?它是 excel 2003 ...
    • @Miles 你是什么意思,它没有按应有的保护?您的宏允许编辑行和列,因为您将它们设置为 True ?我不明白你的问题,你能解释一下吗?
    • 我的ws在没有密码的情况下受到保护......并且仍然没有选择允许单元格、行和列格式(允许使用数据透视表报告是可以的)
    • 所以,当我点击工具、保护 - 取消保护工作表时,不会要求我输入密码...
    • 刚刚编辑了我的 cmets - 我没说格式化单元格也没有被选中(在宏执行之后)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多