修改保护工作表的枢轴柱时出错

时间:2017-02-28 09:00:19

标签: excel vba excel-vba

Sub Pivot_change()

ActiveSheet.Select

Cells.Select

ActiveSheet.protect Password:="SRAS", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

Application.ScreenUpdating = False

Col1 = Cells(10, 1).Value

Col2 = Cells(10, 2).Value

ActiveSheet.PivotTables("PivotTable5").PivotFields(Col1).Orientation = xlHidden

ActiveSheet.PivotTables("PivotTable5").PivotFields(Col2).Orientation = xlHidden

With ActiveSheet.PivotTables("PivotTable5").PivotFields("CU")

.Orientation = xlRowField

.Position = 1

End With

With ActiveSheet.PivotTables("PivotTable5").PivotFields("KAM")

.Orientation = xlRowField

.Position = 2

End With

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

只需在修改后保护工作表:

Sub Pivot_change()
Application.ScreenUpdating = False
With ActiveSheet
    Col1 = .Cells(10, 1).Value
    Col2 = .Cells(10, 2).Value
    With .PivotTables("PivotTable5")
        .PivotFields(Col1).Orientation = xlHidden
        .PivotFields(Col2).Orientation = xlHidden
        With .PivotFields("CU")
            .Orientation = xlRowField
            .Position = 1
        End With '.PivotFields("CU")
        With .PivotFields("KAM")
            .Orientation = xlRowField
            .Position = 2
        End With '.PivotFields("KAM")
    End With '.PivotTables("PivotTable5")
    .Protect Password:="SRAS", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
End With 'ActiveSheet
Application.ScreenUpdating = True
End Sub