Change Pivot Table to new range

A A Ron

New Member
Joined
Feb 1, 2017
Messages
12
I have been trying to tweak this for a long time, and I'm sure there is an easy fix for this, I just haven't figured it out.

My goal is to automatically update the Pivot Table based on the new data in the worksheet with Headings on row 5. Any help would be greatly appreciated. Thank You in advance.

Code:
Sub AdjustPivotDataRange()




Dim Data_sht As Worksheet
Dim Pivot_sht As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String


'Set Variables Equal to Data Sheet and Pivot Sheet
  Set Data_sht = ActiveWorkbook.Worksheets("Completed Projects")
  Set Pivot_sht = ActiveWorkbook.Worksheets("Current Cycle Time BB.MBB")


'Enter in Pivot Table Name
  PivotName = "Pivot7"


'Dynamically Retrieve Range Address of Data
  Set StartPoint = Data_sht.Range("A5")
  Set DataRange = Data_sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))
  
  NewRange = Data_sht.Name & "!" & _
    DataRange.Address(ReferenceStyle:=xlR1C1)


'Make sure every column in data set has a heading and is not blank (error prevention)
  If WorksheetFunction.CountBlank(DataRange.Rows(1)) > 0 Then
    MsgBox "One of your data columns has a blank heading." & vbNewLine _
      & "Please fix and re-run!.", vbCritical, "Column Heading Missing!"
    Exit Sub
  End If


'Change Pivot Table Data Source Range Address
  Pivot_sht.PivotTables(Pivot7).ChangePivotCache _
    ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange)
      
'Ensure Pivot Table is Refreshed
  Pivot_sht.PivotTables(Pivot7).RefreshTable






End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Do you mean you are just adding or deleting rows from a range and the PT should update?
If so, select your range and then Insert Table - Use this Table as your data source
You can insert a small macro for updating when one or other event ( worksheet_activate or whatever you need) happens
Code:
[B]Private Sub Worksheet_Activate()[/B]

    Run "PivotMacro"

End Sub[B][B]Refresh a Single Pivot Table[/B][/B] [B]Sub PivotMacro()[/B]

Dim pt As PivotTable



    Set pt = ActiveSheet.PivotTables("MyPivot")

    pt.RefreshTable

[B]End Sub[/B]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,447
Members
449,453
Latest member
jayeshw

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