I am attempting to filter a "temporary" pivot table on a loop, by the contents of a cell. The pivot table is generated on a worksheet which is removed following pivot table generation, and output to another location. I have attempted to filter this pivot table based on the contents of a cell specified by my loop but have been unsuccessful.The majority of information indicates that a "Private Sub Worksheet_SelectionChange(ByVal Target As Range)" sub must be specified for the worksheet that the pivot table is located in, but since I am inserting my pivot table into a temporary worksheet, I cannot do this.
I'm sure there is a way to reference a cell for a filter and hopeful someone on Eileen's Lounge can help me with my issue!
Below is the part of my relevant code. I have included one pivot table filter and am hopeful I can just insert something in there. If you need more code, please let me know.
Thanks everyone!
Diedrich
Code: Select all
'Insert temporary worksheet for pivot table
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "Temp_Pivot"
'Define worksheet
Dim Temp_Pivot As Worksheet
Set Temp_Pivot = Worksheets("Temp_Pivot")
'Insert pivot table
Dim wb As Workbook
Dim ws As Worksheet
Dim rng1 As Range
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Data")
Set rng1 = ws.Cells.Find("*", ws.[A1], xlFormulas, , , xlPrevious)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & CStr(rng1.Row) & "C" & CStr(rng1.Column), Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=Temp_Pivot.Range("A1"), TableName:="PivotTable31", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Temp_Pivot").Select
Cells(2, 1).Select
'Filter for criteria 1
With ActiveSheet.PivotTables("PivotTable31").PivotFields("Criteria_1")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable31").PivotFields("Criteria_1"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable31").PivotFields("Criteria_1").CurrentPage = _
"Yes"