VBAを使用してExcelでピボットテーブルをフィルタリングしようとするために、インターネットからソリューションを永遠にコピーして貼り付けようとしました。以下のコードは機能しません。
Sub FilterPivotTable()
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable2").ManualUpdate = True
ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").CurrentPage = "K123223"
ActiveSheet.PivotTables("PivotTable2").ManualUpdate = False
Application.ScreenUpdating = True
End Sub
フィルタリングして、SavedFamilyCode K123223 を持つすべての行が表示されるようにします。ピボット テーブルに他の行を表示したくありません。以前のフィルターに関係なく、これを機能させたいです。これで私を助けてくれることを願っています。ありがとう!
あなたの投稿に基づいて、私は試しています:
Sub FilterPivotField()
Dim Field As PivotField
Field = ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode")
Value = Range("$A$2")
Application.ScreenUpdating = False
With Field
If .Orientation = xlPageField Then
.CurrentPage = Value
ElseIf .Orientation = xlRowField Or .Orientation = xlColumnField Then
Dim i As Long
On Error Resume Next ' Needed to avoid getting errors when manipulating fields that were deleted from the data source.
' Set first item to Visible to avoid getting no visible items while working
.PivotItems(1).Visible = True
For i = 2 To Field.PivotItems.Count
If .PivotItems(i).Name = Value Then _
.PivotItems(i).Visible = True Else _
.PivotItems(i).Visible = False
Next i
If .PivotItems(1).Name = Value Then _
.PivotItems(1).Visible = True Else _
.PivotItems(1).Visible = False
End If
End With
Application.ScreenUpdating = True
End Sub
残念ながら、Run time error 91: Object variable or With block variable not set が発生します。このエラーの原因は何ですか?