How to adjust a Pivot Table range in Excel VBA

Cakz Primz

Board Regular
Joined
Dec 4, 2016
Messages
102
Office Version
  1. 365
Platform
  1. Windows
Dear all,

I have 2 sheets, one is "Data", and another is "OWL Balance"
Data is just a common range, from colomn A2 up to column AE until the lastrow, and this is not a table. Number of rows is dynamically changed. Growing up.
Currently, number of rows in "Data" sheet is 3,312 but tomorrow must be higher as some OWL (Outstanding Work List) are closed.
We used that range to create 4 (four) PivotTables in "OWL Balance" sheet, the name of the PivotTable are:
"PivotTable1"
"PivotTable2"
"PivotTable10"
"PivotTable11"

How to adjust the range in all PivotTables based on the latest number of rows in "Data" sheet? without turning the area into a Table
For the moment we do it manually changing data source range, but often we forgot to adjust, so the pivotchart gave the improper number.

Thanks so much in advance, for your kind assistance, help and attention.

Best regards,
Prima - Indonesia
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
First define a dynamic named range (Ribbon >> Formulas tab >> Defined Names group >> Define Name) . . .

VBA Code:
Name:  DataRange 'change the name as desired

Refers to:  =OFFSET(Data!$A$2:$AE$2,0,0,COUNTA(Data!$AE:$AE))

Click OK

Note, however, if cell AE1 contains a value, you'll need to adjust the formula as follows...

VBA Code:
=OFFSET(Data!$A$2:$AE$2,0,0,COUNTA(Data!$AE:$AE)-1)

Then change the data source for each of your PivotTables so that it refers to DataRange as the range.

Hope this helps!
 
Upvote 0
Solution
First define a dynamic named range (Ribbon >> Formulas tab >> Defined Names group >> Define Name) . . .

VBA Code:
Name:  DataRange 'change the name as desired

Refers to:  =OFFSET(Data!$A$2:$AE$2,0,0,COUNTA(Data!$AE:$AE))

Click OK

Note, however, if cell AE1 contains a value, you'll need to adjust the formula as follows...

VBA Code:
=OFFSET(Data!$A$2:$AE$2,0,0,COUNTA(Data!$AE:$AE)-1)

Then change the data source for each of your PivotTables so that it refers to DataRange as the range.

Hope this helps!

Dear Domenic,

Thank you very much for your kind assistance, help.
Your solution is working very well. Really appreciate it.
Problem solved !

Again, thank you very very much.

Best regards,
Prima - Indonesia
 
Upvote 0
You're very welcome, I'm glad I could help.

And thanks for your feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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