我无法在数据透视表中为B3过滤器选择多个值。当我将条件输入为数组时,它会将其识别为null-“无法在数据透视表报表中输入null值作为项目或字段名称。”有没有简单的方法可以做到这一点?我找不到Google提供的解决方案。
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
答案 0 :(得分:2)
点击“录制宏”,然后执行操作,然后点击“停止录制”。
您应该具有以下内容:
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