Excel Macro code to Auto Update Pivot Table Range

How to Auto Update Pivot Table Range in excel sheet through VBA Macro Code

To use VBA macro code in an Excel spreadsheet, you need to do the following:

  1. Open the Excel spreadsheet where you want to use the macro code.

  2. Press “Alt + F11” to open the VBA developer window.

  3. In the VBA developer window, click the “Insert” menu and select “Module” to create a new module.

  4. Paste the VBA macro code into the module window.

  5. To run the macro, click the “Run” button in the VBA developer window or press “F5” on your keyboard.

Alternatively, you can also create a button or a shortcut key to run the macro. To create a button, go to the “Developer” tab in the main Excel window and click the “Insert” button. Then select a button control and draw it on the worksheet. Right-click the button and select “Assign Macro” to assign the macro code to the button. To create a shortcut key, go to the “Macros” tab in the VBA developer window, select the macro from the list, and click the “Options” button. Then, enter a shortcut key in the “Shortcut key” field.

Note: If you are running the macro for the first time, you may need to enable macros in Excel by going to the “File” tab and selecting “Options.” Under the “Trust Center” settings, click the “Trust Center Settings” button and go to the “Macro Settings” tab. Select “Enable all macros” and click “OK.”

Copy Below VBA Macro Code for Auto Update Pivot Table Range
Sub UpdatePivotTableRange()
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("PivotTableData3")
Set Pivot_Sheet = ThisWorkbook.Worksheets("Pivot3")
'Enter in Pivot Table Name
PivotName = "PivotTable2"
'Defining Staring Point & Dynamic Range
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
'Change Pivot Table Data Source Range Address
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
'Ensure Pivot Table is Refreshed
'Complete Message
MsgBox "Your Pivot Table is now updated."
End Sub

If you are not using Excel tables then you can use this code to update pivot table range.


Work Order Invoice Format

Work Order Invoice

Work Order Invoice Template: Efficient and Detailed Billing for Specific Tasks In businesses where tasks are clearly defined and structured, …

Download Now
Work Contract Service Invoice Template

Work Contract Service Invoice

Work Contract Service Invoice Template: Ensuring Transparency in Contractual Billing In the realm of contractual services, clear communication, and precise …

Download Now
US Customs Invoice

US Customs Invoice

US Customs Invoice Template: A Key to Unlocking Smooth International Transactions Engaging in international trade demands meticulous adherence to various …

Download Now
UK VAT Invoice Template

UK VAT Invoice Template

UK VAT Invoice Template: The Gold Standard for Value-Added Tax Billing Navigating the maze of tax regulations can be complex, …

Download Now
Tree Trimming Invoice Format

Tree Trimming Invoice

Tree Trimming Invoice Format: Detailed Billing for Tree Care Projects Tree trimming and care is an art that requires precision, …

Download Now
Task Order Billing Format

Task Order Billing Format

Task Order Billing Format Template: Your Key to Efficient Milestone-Based Invoicing In a landscape where project dynamics shift and evolve, …

Download Now