Copy filtered pivot table data to new sheets instead of new workbooks

Martin sherk

Board Regular
Joined
Sep 11, 2022
Messages
94
Office Version
  1. 365
  2. 2016
I've come so far in my project and it all depends on solving my last issue, the below code helps me create a Pivot table for all my customers based on company code, so it filters company code criteria in the pivot table and then copies the filtered data to new workbooks named after the company code.

All I need is that instead of copying data to the new workbook, I want it to be copied to the current workbook (the workbook with the pivot table on), just adding new sheets named after the company code and copy the filtered data in it.

Wish someone would help me with this, I would be so thnakful.

My code:

VBA Code:
Option Explicit
Sub GetAllEmployeeSelections()

    'Const filePath As String = "C:\Users\User\Desktop\" 'save location for new files

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim pvt As PivotTable


    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("PivotTable")
    Set pvt = ws.PivotTables("PivotTable1")
pvt.PivotCache.MissingItemsLimit = xlMissingItemsNone

    Application.ScreenUpdating = False

    Dim pvtField As PivotField
    Dim item As Long
    Dim item2 As Long

    Set pvtField = pvt.PivotFields("Company Code")

    For item = 1 To pvtField.PivotItems.Count

          pvtField.PivotItems(item).Visible = True

          For item2 = 1 To pvtField.PivotItems.Count

              If item2 <> item Then pvtField.PivotItems(item2).Visible = False

          Next item2

        Dim newBook As Workbook
        Set newBook = Workbooks.Add

        With newBook

            Dim currentName As String
            currentName = pvtField.PivotItems(item).Name

            .Worksheets(1).Name = currentName

            pvt.TableRange2.Copy

            Worksheets(currentName).Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

        End With

    Next item

    Application.ScreenUpdating = True

End Sub
 
I'm afraid you need to post that as a new thread.

Assuming your company name is USA1, I would be naming the pivot sheet "USA1 PVT"
That way when the pivot's are created it can just add PVT to the company name.
Then store the company name and copy all the sheets starting with the Company Name ie USA1 to a new workbook.

I would definitely not be copying the whole Pivot and would go back to copying it as Values only.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I'm afraid you need to post that as a new thread.

Assuming your company name is USA1, I would be naming the pivot sheet "USA1 PVT"
That way when the pivot's are created it can just add PVT to the company name.
Then store the company name and copy all the sheets starting with the Company Name ie USA1 to a new workbook.

I would definitely not be copying the whole Pivot and would go back to copying it as Values only.
Yes, sir, I've just created a completely new thread, I would appreciate it if you can check it out.

thanks!
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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