Martin sherk
Board Regular
- Joined
- Sep 11, 2022
- Messages
- 94
- Office Version
- 365
- 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:
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