VBA - Copied Workbook, Pivot Data Source not Updating

%3CLINGO-SUB%20id%3D%22lingo-sub-2594511%22%20slang%3D%22en-US%22%3EVBA%20-%20Copied%20Workbook%2C%20Pivot%20Data%20Source%20not%20Updating%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2594511%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20writing%20a%20macro%20that%20takes%20a%20master%20file%2C%20takes%20a%20list%20of%20sales%20people%2C%20and%20for%20each%20sales%20person%20(RSL)%20creates%20individual%20copies%20of%20the%20master%20file%20and%20edits%20them%20so%20that%20they%20only%20see%20their%20own%20data.%20I've%20gotten%20everything%20to%20work%20but%20the%20only%20thing%20I%20haven't%20figured%20out%20is%20how%20to%20update%20the%20Pivot%20table%20data%20sources.%20When%20I%20make%20a%20copy%20of%20the%20workbook%2C%20the%20pivot%20data%20source%20stays%20linked%20to%20the%20original%20master%20instead%20of%20updating%20to%20the%20new%20workbook%20(which%20I%20am%20then%20editing).%20Is%20there%20any%20way%20to%20get%20around%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20what%20the%20new%20workbook's%20pivot%20table%20data%20source%20points%20too%2C%20it%20points%20to%20the%20old%20master%20file%20rather%20than%20just%20the%20Sales%20table.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22DKoontz_0-1627509644404.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F299114i784A1B0B59055945%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22DKoontz_0-1627509644404.png%22%20alt%3D%22DKoontz_0-1627509644404.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI'm%20still%20pretty%20new%20to%20VBA%20so%20any%20help%20is%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20code%20I'm%20using%20(it%20still%20needs%20some%20work)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20CreateTracings()%0A%0ADim%20Graphs%20As%20Worksheet%3A%20Set%20Graphs%20%3D%20Sheets(%22Graphs%22)%0A%0ADim%20new_wb%20As%20Workbook%0ADim%20UserName%20As%20String%0ADim%20myFolder%20As%20String%0ADim%20rslName%20As%20String%0A'Dim%20SalesRange%20As%20Range%0A'Dim%20HardwareRange%20As%20Range%0ADim%20lastRow%20As%20Range%0A%0AApplication.ScreenUpdating%20%3D%20False%0AApplication.DisplayAlerts%20%3D%20False%0A%0A'Begin%20loop%0A'------------------------------------------------------------------------------%7C%0AFor%20i%20%3D%201%20To%20Graphs.Range(%22BA%22%20%26amp%3B%20Graphs.Rows.Count).End(xlUp).Row%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20%0A%20%20%20%20rslName%20%3D%20Graphs.Range(%22BA%22%20%26amp%3B%20i)%0A%20%20%20%20%0A%20%20%20%20'Copy%20workbook%0A%20%20%20%20ActiveWorkbook.Sheets.Copy%0A%20%20%20%20Set%20new_wb%20%3D%20ActiveWorkbook%0A%20%20%20%20%0A%20%20%20%20'Edit%20new%20workbook%0A%20%20%20%20'*****************************************************%0A%20%20%20%20'Delete%20Sales%20Region%20Trending%20Tab%0A%20%20%20%20For%20Each%20Sheet%20In%20new_wb.Worksheets%0A%20%20%20%20%20%20%20%20%20If%20Sheet.Name%20%3D%20%22Sales%20Region%20Trending%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20Sheet.Delete%0A%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20Sheet%0A%20%20%20%0A%20%20%20%20'Filter%20and%20delete%20in%20Sales%20Data%0A%20%20%20%20Dim%20SalesRange%20As%20Range%0A%20%20%20%20Set%20SalesRange%20%3D%20ActiveWorkbook.Sheets(%22Sales%20Data-No%20Hardware%22).Range(%22B2%3AS%22%20%26amp%3B%20%5BSalesData%5D.Cells(%5BSalesData%5D.Rows.Count%2C%202).End(xlUp).Row)%0A%20%20%20%20SalesRange.AutoFilter%20Field%3A%3D14%2C%20Criteria1%3A%3D%22%26lt%3B%26gt%3B%22%20%26amp%3B%20rslName%0A%20%20%20%20%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20%20%20%20%20With%20SalesRange%0A%20%20%20%20%20%20%20%20%20%20%20%20.Offset(1).Resize(.Rows.Count%20-%201).SpecialCells(xlCellTypeVisible).EntireRow.Delete%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20%20%20%20%20Err.Clear%0A%20%20%20%20%0A%20%20%20%20'Filter%20and%20delete%20hardware%20data%0A%20%20%20%20Dim%20HardwareRange%20As%20Range%0A%20%20%20%20Set%20HardwareRange%20%3D%20ActiveWorkbook.Sheets(%22Hardware%20Data%22).Range(%22A1%3AQ%22%20%26amp%3B%20%5BHardwareData%5D.Cells(%5BHardwareData%5D.Rows.Count%2C%201).End(xlUp).Row)%0A%20%20%20HardwareRange.AutoFilter%20Field%3A%3D14%2C%20Criteria1%3A%3D%22%26lt%3B%26gt%3B%22%20%26amp%3B%20rslName%0A%20%20%20%20%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20%20%20%20%20With%20HardwareRange%0A%20%20%20%20%20%20%20%20%20%20%20%20.Offset(1).Resize(.Rows.Count%20-%200).SpecialCells(xlCellTypeVisible).EntireRow.Delete%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20%20%20%20%20Err.Clear%0A%20%20%20%20%0A%20%20%20%20'Refresh%20all%20pivot%20tables%0A%20%20%20%20Calculate%0A%20%20%20%20ActiveWorkbook.RefreshAll%0A%20%20%20%20%0A%20%20%20%20'Saving%20tracing%20copy%0A%20%20%20%20UserName%20%3D%20Environ(%22Username%22)%0A%20%20%20%20myFolder%20%3D%20%22C%3A%5CUsers%5C%22%20%26amp%3B%20UserName%20%26amp%3B%20%22%5CDesktop%5CField%20Tracings%5C%22%0A%20%20%20%20%0A%20%20%20%20'Creates%20Field%20Tracings%20folder%20if%20missing%0A%20%20%20%20If%20Dir(myFolder%2C%20vbDirectory)%20%3D%20%22%22%20Then%0A%20%20%20%20%20%20%20%20%20MkDir%20myFolder%0A%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20'Saves%20Active%20workbook%0A%20%20%20%20ActiveWorkbook.SaveAs%20Filename%3A%3D%22C%3A%5CUsers%5C%22%20%26amp%3B%20UserName%20%26amp%3B%20%22%5CDesktop%5CField%20Tracings%5C%22%20%26amp%3B%20rslName%2C%20FileFormat%3A%3DxlOpenXMLWorkbookMacroEnabled%0A%20%20%20%20%0A%20%20%20%20'Close%20activeworkbook%0A%20%20%20%20ActiveWorkbook.Close%20False%0A%20%20%20%20%0AOn%20Error%20GoTo%200%0ANext%20i%0A'------------------------------------------------------------------------------%7C%0A'End%20Loop%0A%0AApplication.ScreenUpdating%20%3D%20True%0AApplication.DisplayAlerts%20%3D%20True%0A%20%20%20%20%0A%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2594511%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2595568%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Copied%20Workbook%2C%20Pivot%20Data%20Source%20not%20Updating%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2595568%22%20slang%3D%22en-US%22%3EPlease%20let%20me%20know%20if%20any%20additional%20information%20would%20be%20helpful!%3CBR%20%2F%3EThis%20is%20similar%20a%20similar%20problem%20to%20this%20question%20I%20believe%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fpivot-table-data-source-remains-fixed-when-copying-sheets%2Fm-p%2F2594561%23M108538%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Fpivot-table-data-source-remains-fixed-when-copying-sheets%2Fm-p%2F2594561%23M108538%3C%2FA%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

Hi,

 

I'm writing a macro that takes a master file, takes a list of sales people, and for each sales person (RSL) creates individual copies of the master file and edits them so that they only see their own data. I've gotten everything to work but the only thing I haven't figured out is how to update the Pivot table data sources. When I make a copy of the workbook, the pivot data source stays linked to the original master instead of updating to the new workbook (which I am then editing). Is there any way to get around this?

 

This is what the new workbook's pivot table data source points too, it points to the old master file rather than just the Sales table.

DKoontz_0-1627509644404.png

I'm still pretty new to VBA so any help is greatly appreciated.

 

Here is the code I'm using (it still needs some work)

 

 

Sub CreateTracings()

Dim Graphs As Worksheet: Set Graphs = Sheets("Graphs")

Dim new_wb As Workbook
Dim UserName As String
Dim myFolder As String
Dim rslName As String
'Dim SalesRange As Range
'Dim HardwareRange As Range
Dim lastRow As Range

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Begin loop
'------------------------------------------------------------------------------|
For i = 1 To Graphs.Range("BA" & Graphs.Rows.Count).End(xlUp).Row
    On Error Resume Next
    
    rslName = Graphs.Range("BA" & i)
    
    'Copy workbook
    ActiveWorkbook.Sheets.Copy
    Set new_wb = ActiveWorkbook
    
    'Edit new workbook
    '*****************************************************
    'Delete Sales Region Trending Tab
    For Each Sheet In new_wb.Worksheets
         If Sheet.Name = "Sales Region Trending" Then
              Sheet.Delete
         End If
    Next Sheet
   
    'Filter and delete in Sales Data
    Dim SalesRange As Range
    Set SalesRange = ActiveWorkbook.Sheets("Sales Data-No Hardware").Range("B2:S" & [SalesData].Cells([SalesData].Rows.Count, 2).End(xlUp).Row)
    SalesRange.AutoFilter Field:=14, Criteria1:="<>" & rslName
    
    On Error Resume Next
        With SalesRange
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
            Err.Clear
    
    'Filter and delete hardware data
    Dim HardwareRange As Range
    Set HardwareRange = ActiveWorkbook.Sheets("Hardware Data").Range("A1:Q" & [HardwareData].Cells([HardwareData].Rows.Count, 1).End(xlUp).Row)
   HardwareRange.AutoFilter Field:=14, Criteria1:="<>" & rslName
    
    On Error Resume Next
        With HardwareRange
            .Offset(1).Resize(.Rows.Count - 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
            Err.Clear
    
    'Refresh all pivot tables
    Calculate
    ActiveWorkbook.RefreshAll
    
    'Saving tracing copy
    UserName = Environ("Username")
    myFolder = "C:\Users\" & UserName & "\Desktop\Field Tracings\"
    
    'Creates Field Tracings folder if missing
    If Dir(myFolder, vbDirectory) = "" Then
         MkDir myFolder
    End If
        
    'Saves Active workbook
    ActiveWorkbook.SaveAs Filename:="C:\Users\" & UserName & "\Desktop\Field Tracings\" & rslName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    
    'Close activeworkbook
    ActiveWorkbook.Close False
    
On Error GoTo 0
Next i
'------------------------------------------------------------------------------|
'End Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True
    
    
End Sub

 

 

  

0 Replies