簡體   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