How can my excel macro update and refresh pivot table

Viewed 25

I am using the below code to copy files from a folder on my i drive into the correcposning worksheet in an excel workbook. After the files are copied in I have to manually update the pivot table data source and refresh each pivot table. Does anyone know how I could make this happen within the macro?

Sub Update_Data()
 
 Dim fileNames(12) As String
 Dim WorksheetNames(12) As String
 Dim i As Integer
 Dim currentFileName As String
 
 fileNames(0) = "ActiveCount_L2_Report"
 fileNames(1) = "Apps_Per_Agent_L2" 'does not end in report
 'fileNames(2) = "Cancel_Count_L2_Report"
 fileNames(2) = "Data_Active_L2_Report"
 fileNames(3) = "NonRapidDisenroll_count_L2" 'does not end in report
 fileNames(4) = "RapidDisenroll_count_L2" 'does not end in report
 'fileNames(5) = "RTSPercentages_L2_Report"
 fileNames(5) = "SOLD_ENROLLED_Count_L2_Report"
 fileNames(6) = "Sold_Policy_Count_L2_Report"
 fileNames(7) = "TOH_NAME_L2_Report"
 fileNames(8) = "TOH_DATA_L2_Report"
 fileNames(9) = "RTSPercentages_L2_2023" 'does not end in report
 fileNames(10) = "RTS Distribution -Master Contact List" 'does not end in report
 
 WorksheetNames(0) = "Active_Count_L2"
 WorksheetNames(1) = "Apps_Per_Agent_L2"
 'WorksheetNames(2) = "Cancel_Count_L2"
 WorksheetNames(2) = "DATA_Active_L2"
 WorksheetNames(3) = "NonRapidDisenroll_count_L2"
 WorksheetNames(4) = "RapidDisenroll_count_L2"
 'WorksheetNames(5) = "RTSPercentages_L2"
 WorksheetNames(5) = "SOLD_ENROLLED_Count_L2"
 WorksheetNames(6) = "Sold_Policy_Count_L2"
 WorksheetNames(7) = "TOH_NAME_L2"
 WorksheetNames(8) = "TOH_DATA_L2"
 WorksheetNames(9) = "RTSPercentages_L2_2023"
 WorksheetNames(10) = "Distribution List"
        
 
 For i = 0 To 10
    '….Groups is the name of the tab I need to update the data for….

    '… assigning nameoffile variable as the current wb
        Scorecard = ActiveWorkbook.Name
        
    'putting the used range into a variable (# of rows used)
       clear_range = Sheets(WorksheetNames(i)).Cells(Rows.Count, "A").End(xlUp).Row
       'clear_range = Sheets("ActiveCount_L2").Cells(Rows.Count, "A").End(xlUp).Row
        
    '.. If there's data in Groups tab (aside from the headers) then delete it out before bringing in new data
        If clear_range > 1 Then
            Sheets(WorksheetNames(i)).Range("A2:G" & clear_range).Delete
            'Sheets("ActiveCount_L2").Range("A2:G" & clear_range).Delete
        End If
        
    'Path to folder where data file exists..
         folderPath = "I:\Health Business Operations\Channel Service Organization\DARU\01 - Reporting Tools\Medicare Sales Agencies\LVL2 Scorecard Files"
    
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

    '…add in name of data file here
                
     'Filename = Dir(folderPath & "ActiveCount_L2_Report.xlsx*")
     currentFileName = fileNames(i)
     
     'Filename = Dir(folderPath & currentFileName)
     Filename = folderPath & currentFileName
     
     If i = 1 And i = 4 And i = 5 And i = 11 Then
      Filename = Filename & "_Report"
     End If
     
     Filename = Filename & ".xlsx*"
     
     Do While Filename <> ""
 
                    Application.ScreenUpdating = False
                    Application.DisplayAlerts = False
                    Application.AskToUpdateLinks = False
           
    'open up data file

    Set wb = Workbooks.Open(Filename)
            
    'determine the used range
            
         used_range = Cells(Rows.Count, "A").End(xlUp).Row
    '    Range("A6:B" & used_range & ",d6:d" & used_range).Copy
    'copy over the used range (will need to specify columns, this file had data in cols A:F)..
                
                Range("A2:AE" & used_range).Copy

'activate the original workbook (that this macro should be in..
 
            Windows(Scorecard).Activate

'..determine used range in group tab (Should be 1 bc it was cleared out above). Using range +1 because we want to 'paste the data 1 row below already used range
          
            used_range_gps = Sheets(WorksheetNames(i)).Cells(Rows.Count, "A").End(xlUp).Row + 1
            'used_range_gps = Sheets("ActiveCount_L2").Cells(Rows.Count, "A").End(xlUp).Row + 1

'..pasting the data in the group tab at column A and whatever row the used_range_gps is
            
            Sheets(WorksheetNames(i)).Range("A2:AE" & used_range_gps).PasteSpecial xlValues
            'Sheets("ActiveCount_L2").Range("A2:AE" & used_range_gps).PasteSpecial xlValues
      
      'Filename = Dir
      wb.Close False
      Filename = ""
     Loop
  Next i
End Sub
0 Answers
Related