• Microsoft Office
  • Microsoft Windows
  • Other Software
    • Microsoft Visual
    • Microsoft Project
    • Microsoft Visio
  • Anti Virus
  • Blog
    • Word
    • Excel
    • Powerpoint
    • Software tricks/tips
  • POLICIES
    • PAYMENT GUIDE
    • SHIPPING POLICY
    • REFUND POLICY
    • TERMS & CONDITIONS
    • Contact Us

No products in the cart.

  • Microsoft Office
  • Microsoft Windows
  • Other Software
    • Microsoft Visual
    • Microsoft Project
    • Microsoft Visio
  • Anti Virus
  • Blog
    • Word
    • Excel
    • Powerpoint
    • Software tricks/tips
  • POLICIES
    • PAYMENT GUIDE
    • SHIPPING POLICY
    • REFUND POLICY
    • TERMS & CONDITIONS
    • Contact Us

No products in the cart.

  • Microsoft Office
  • Microsoft Windows
  • Other Software
    • Microsoft Visual
    • Microsoft Project
    • Microsoft Visio
  • Anti Virus
  • Blog
    • Word
    • Excel
    • Powerpoint
    • Software tricks/tips
  • POLICIES
    • PAYMENT GUIDE
    • SHIPPING POLICY
    • REFUND POLICY
    • TERMS & CONDITIONS
    • Contact Us

No products in the cart.

  • Microsoft Office
  • Microsoft Windows
  • Other Software
    • Microsoft Visual
    • Microsoft Project
    • Microsoft Visio
  • Anti Virus
  • Blog
    • Word
    • Excel
    • Powerpoint
    • Software tricks/tips
  • POLICIES
    • PAYMENT GUIDE
    • SHIPPING POLICY
    • REFUND POLICY
    • TERMS & CONDITIONS
    • Contact Us
Excel

Updating Pivot Table Data Source Range Dynamically in Excel

0 Comments

Updating Pivot Table Data Source Range Dynamically in Excel. In Excel, we have various techniques like Excel Tables or Dynamic Named Ranges to dynamically change or update pivot tables. However, these methods may not be foolproof as they still require manual refreshing of the pivot table. Additionally, if you have a large dataset with thousands of rows and columns, using Excel Tables can make the file heavy and slow. In such cases, the most reliable way to automate the process is by using VBA (Visual Basic for Applications).

Updating Pivot Table Data Source Range Dynamically in Excel

Updating Pivot Table Data Source Range Dynamically in Excel

Updating Pivot Table Data Source Range Dynamically in Excel

In this article, we will explore how to automatically change the data source of a pivot table and instantly reflect any new rows or columns added to the source data. To achieve this, we will utilize sheet modules and worksheet events in VBA.

First, open the Excel file and press ALT+F11 to open the Visual Basic Editor. In the Project Explorer, locate the sheet that contains the source data and double-click on it to open its coding area.

Updating Pivot Table Data Source Range Dynamically in Excel

Updating Pivot Table Data Source Range Dynamically in Excel

Within the coding area, select the worksheet from the left drop-down menu and then choose the “Deactivate” event. This will create a blank sub named “Worksheet_Deactivate”. We will place our code for dynamically changing the data source and refreshing the pivot table within this block of code. The code will run whenever you switch from the data sheet to any other sheet.

Now, let’s implement the code to update the pivot table dynamically with a new range. The code below is an example for demonstration purposes:

Private Sub Worksheet_Deactivate()

    Dim pt As PivotTable

    Dim pc As PivotCache

    Dim source_data As Range

    

    ‘ Determine the last row and last column of the source data

    lstrow = Cells(Rows.Count, 1).End(xlUp).Row

    lstcol = Cells(1, Columns.Count).End(xlToLeft).Column

    

    ‘ Set the range for the source data

    Set source_data = Range(Cells(1, 1), Cells(lstrow, lstcol))

    

    ‘ Create a new pivot cache with the updated source data

    Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, SourceData:=source_data)

    

    ‘ Specify the pivot table to be updated (change the sheet and pivot table name as required)

    Set pt = Sheet2.PivotTables(“PivotTable1”)

    

    ‘ Update the pivot table’s cache with the new source data

    pt.ChangePivotCache pc

End Sub

Updating Pivot Table Data Source Range Dynamically in Excel

Updating Pivot Table Data Source Range Dynamically in Excel

In this code, we utilize the “Worksheet_Deactivate” event so that the code runs to update the pivot table whenever we switch from the source data sheet. The code determines the last row and last column of the source data dynamically. It then sets the range for the source data using these values.

Updating Pivot Table Data Source Range Dynamically in Excel

Updating Pivot Table Data Source Range Dynamically in Excel

A new pivot cache is created with the updated source data. The pivot table to be updated is specified (modify the sheet and pivot table name as needed), and the pivot table’s cache is updated with the new source data using the ChangePivotCache method.Pivot Table Data 5

By implementing this code, the pivot table will automatically update whenever there are changes in the source data. If you have multiple pivot tables using the same data source, you can use the same cache in each pivot table object.

That’s how you can dynamically change the data source range in Excel using VBA. I hope this explanation was clear. If you have any further questions, feel free to ask in the comments section below.

Rate this post
27
283 Views
Wrapping Text in ExcelPrevWrapping Text in ExcelMay 30, 2023
Extracting First, Middle, and Last Names in Excel 2010May 30, 2023Extracting First, Middle, and Last Names in Excel 2010Next

Leave a Reply Cancel reply

You must be logged in to post a comment.

Buy Windows 11 Professional MS Products CD Key
Buy Office 2021 Professional Plus Key Global For 5 PC
Top rated products
  • Trend Micro Internet Security 3 Devices 1 Year Key GLOBAL Trend Micro Internet Security 3 Devices 1 Year Key GLOBAL
    Rated 5.00 out of 5
    $17.00
  • Windows 11 Pro Key Global Windows 11 Pro Key Global
    Rated 5.00 out of 5
    $6.00
  • AVG Internet Security 2021 10 Devices 2 Years Global AVG Internet Security 2021 10 Devices 2 Years Global
    Rated 5.00 out of 5
    $42.20
  • Trend Micro Maximum Security 3 Devices 1 Year key Global Trend Micro Maximum Security 3 Devices 1 Year key Global
    Rated 5.00 out of 5
    $15.00
  • Windows 11 Home Key Global 2021 Windows 11 Home Key Global 2021
    Rated 5.00 out of 5
    $6.00
Products
  • Buy Windows Server 2016 Essentials Key Global Buy Windows Server 2016 Essentials Key Global
    Rated 4.75 out of 5
    $10.00
  • Windows Server 2008 R2 Standard Windows Server 2008 R2 Standard $10.00
  • Windows Server 2022 Standard Key Global Windows Server 2022 Standard Key Global
    Rated 4.10 out of 5
    $7.00
  • Avast Ultimate Suite 2021 3 Years 10 Devices Global Avast Ultimate Suite 2021 3 Years 10 Devices Global
    Rated 5.00 out of 5
    $90.00
  • Microsoft Visual Studio Enterprise 2019 For 1 PC Microsoft Visual Studio Enterprise 2019 For 1 PC
    Rated 4.88 out of 5
    $15.00
  • Microsoft Visual Studio 2022 Professional Key Microsoft Visual Studio 2022 Professional Key $19.00
  • AVG Ultimate 2021 with Antivirus + Cleaner, Secure VPN 10 Devices 1 Year AVG Ultimate 2021 with Antivirus + Cleaner, Secure VPN 10 Devices 1 Year
    Rated 4.91 out of 5
    $34.00
  • Microsoft Visio Professional 2016 Key 1PC Microsoft Visio Professional 2016 Key 1PC $9.00
  • Windows Server 2012 R2 Datacenter Key Global Windows Server 2012 R2 Datacenter Key Global
    Rated 4.71 out of 5
    $15.00
  • Project Professional 2013 Microsoft Project Professional 2013 - 1 PC $9.00
Product categories
  • Anti Virus
  • Microsoft Office
  • Microsoft Project
  • Microsoft Visio
  • Microsoft Visual
  • Microsoft Windows
  • Other Software
  • Uncategorized

Buffcom.net always brings the best digital products and services to you. Specializing in Office Software and online marketing services

BIG SALE 50% IN MAY

Microsoft Office
Microsoft Windows
Anti-Virus
Contact Us

Visit Us:

125 Division St, New York, NY 10002, USA

Mail Us:

buffcom.net@gmail.com

TERMS & CONDITIONS | PAYMENT GUIDE  | SHIPPING POLICY  | REFUND POLICY

Copyright © 2019 buffcom.net  All Rights Reserved.