trying to loop creation of a filtered sheet based on a pivot table in VBA, but getting nowhere

FossilAmmonite

New Member
Joined
Sep 2, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hey there everyone, long time lurker, but first time where im really scratching my head with this one and the MSDN documentation isnt really giving me much of a lead to work off of here.
So I had someone who created a macro that was taking pivot table data, and creating a bunch of sheets for each individual user. since it was just a recorded macro it was super rudimentary and was missing any form of logic just in case the pivot table rows grew or shrank, so i decided to build out a loop to manage it better.
Pretty much what they get when they run the macro is X amount of sheets created, each with the user filtered(as well as a field), but im getting nowhere when i try to put it in a loop. I am doing something wrong with my pivot table items, as I keep having mismatched objects, but I am just wrapped up with confusion on what its supposed to look like. Frankly, im not too sure how the macro worked in the first place, so im definitely missing something here.
Here is one of the commands in the macro(it would do this down the line of each of the cells in that row). this confirmed works.
VBA Code:
Range("B4").Select
Selection.ShowDetail = True
Cells.Select
Cells.EntireColumn.AutoFit
ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=5, Criteria1:= _
      "Param"
Sheets("Sheet2").Select

Here is my loop im trying to make here(im guessing there are multiple problems, but its breaking on the range i think, but i really dont understand how I get that range to be proper as the pivot item usually seems to just return the name of the field (i.e. the data inside the field and none of the actual cell locations or anything)

VBA Code:
Set pt = ActiveSheet.PivotTables("PivotTable1")
Set pits = pt.PivotFields("Employee Name").PivotItems
For Each pit In pits
     Sheets("Sheet2").Select
     Range (Cells.PivotCell.PivotItem(pit))
     Cells.Select
     Cells.EntireColumn.AutoFit
     ActiveSheet.ListObjects(CStr(pit)).Range.AutoFilter Field:=5, Criteria1:= _
          "Param"
Next pit
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top