Loop through a pivot table filter and copy the sheet

onche414

New Member
Joined
Aug 7, 2023
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a pivot table in a worksheet and I would like to create a VBA code that will go select each value in the filter "ID", then copy the sheet and rename it with the value in the ID cell:
1724323351902.png


So for example, it will select store a, copy the sheet and rename it "store a", then select the second value which is store b, copy the sheet and rename it store b etc

Thank you !
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I came up with this code but I am getting an error at the line "set source". Run time error '1004'

VBA Code:
Sub DupliSheets()
Dim datarange As Range, source As Range, c As Range, sh2 As Worksheet

Set datarange = Worksheets("New Template").Range("B1")
Set sh2 = Sheets("New Template")
Set source = Evaluate(datarange.Validation.Formula1)

For Each c In source
    datarange.Value = c.Value
    sh2.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = c.Value
    
Next
End Sub

Can anyone help please ?
 
Upvote 0
Try the following...

VBA Code:
Sub DupliSheets()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim rng As Range
    Dim pt As PivotTable
    Dim pi As PivotItem
    
    Set wb = ThisWorkbook
    
    Set sht = wb.Worksheets("New Template")
    
    Set rng = sht.Range("B1")
    
    Set pt = sht.PivotTables("PivotTable1") 'change the name accordingly
    
    With pt
        .ClearAllFilters
        For Each pi In .PivotFields("ID").PivotItems
            rng.Value = pi.Value
            sht.Copy after:=wb.Sheets(wb.Sheets.Count)
            ActiveSheet.Name = pi.Value
        Next pi
        .ClearAllFilters
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Try the following...

VBA Code:
Sub DupliSheets()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim rng As Range
    Dim pt As PivotTable
    Dim pi As PivotItem
   
    Set wb = ThisWorkbook
   
    Set sht = wb.Worksheets("New Template")
   
    Set rng = sht.Range("B1")
   
    Set pt = sht.PivotTables("PivotTable1") 'change the name accordingly
   
    With pt
        .ClearAllFilters
        For Each pi In .PivotFields("ID").PivotItems
            rng.Value = pi.Value
            sht.Copy after:=wb.Sheets(wb.Sheets.Count)
            ActiveSheet.Name = pi.Value
        Next pi
        .ClearAllFilters
    End With
   
End Sub

Hope this helps!

Worked perfectly, thanks !
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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