我无法使用数据透视表为B3过滤器选择多个值。当我将条件作为数组输入时,它将其识别为null - “无法在数据透视表报表中输入空值作为项目或字段名称。”有一种直截了当的方法吗?我找不到谷歌的解决方案。
Sub Button5_Click()
Dim docworksheet As Worksheet
Dim docworkbook As Workbook
Set docworkbook = ThisWorkbook
Set docworksheet = docworkbook.Sheets("Analysis")
docworksheet.Activate
ActiveSheet.PivotTables("PivotTable2").ManualUpdate = False
ActiveSheet.Range("B4").Value = "(blank)"
ActiveSheet.Range("B5").Value = "(All)"
ActiveSheet.Range("B2").Value = "(All)"
ActiveSheet.Range("B3").Value = Array("A", "B", "C")
ActiveSheet.Range("B7").Value = "L"
End Sub
点击“录制宏”,然后执行您的操作,然后点击“停止录制”。
你应该有这样的东西:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Field Name Here")
.PivotItems("First Item Name").Visible = True
.PivotItems("Second Item Name").Visible = False
.PivotItems("Third Item Name").Visible = True
End With
这将逐个进行并设置每个项目。
因此,循环遍历PivotItems
中的所有PivotField
,并与Array
中的值进行比较(例如使用Filter
function) - 对于这样的事情:
Public Sub FilterPivotByArray(Target As PivotField, Values() As String)
Dim piTMP As PivotItem, bManualUpdate As Boolean
With Target.Parent
bManualUpdate = .ManualUpdate 'Store setting for later
.ManualUpdate = True 'Turn on Manual Update to speed things up
End With
Target.ClearAllFilters
For Each piTMP In Target.PivotItems
'Visible if Value is in Array
'Not Visible if Value is Not in Array
piTMP.Visible = (UBound(Filter(Values, piTMP.Value)) >= 0)
Next piTMP
With Target.Parent
.Update
.ManualUpdate = bManualUpdate 'restore setting
End With
End Sub