我将一些代码放在一起,这些代码根据下拉菜单中的选择更改某些数据透视表的行和列字段。
无论我将其放置在代码中的任何地方,无论多少次,我似乎都无法使application.screenupdating = False
正常工作。
有没有一种方法可以清理掉这段代码,因为我基本上已经从下拉列表中为每个选择复制了一部分代码。
以下代码嵌入在我正在使用的工作表中:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pvt As pivotTable
Dim pf As String
Dim pf_Name As String
Dim pvtf As PivotField
Set pvt = Sheets("Exit Interviews").PivotTables("Question")
Set pvt2 = Sheets("Exit Interviews").PivotTables("Question2")
Application.ScreenUpdating = False
If Target.Address = "$B$2" Then
'This section is duplicated for each drop down selection //
If Range("$B$2").Value = "Question1" Then
On Error Resume Next
For Each pvtf In pvt2.ColumnFields
pvtf.Orientation = xlHidden
Next pvtf
pf = "Question1"
pf_Name = "Question1 "
pvt2.AddDataField pvt.PivotFields("Question1"), pf_Name, xlSum
pvt2.PivotFields("Question1 ").Orientation = xlColumnField
For Each pvtf In pvt.RowFields
pvtf.Orientation = xlHidden
Next pvtf
pf = "Question1"
pf_Name = "Question1 "
pvt.AddDataField pvt.PivotFields("Question1"), pf_Name, xlSum
pvt.PivotFields("Question1 ").Orientation = xlRowField
'//
End If
End If
End Sub