Data range in Pivot Table with VBA statement

Hans810

Board Regular
Joined
Dec 1, 2005
Messages
63
Good evening all,

I have the following situation:
From a data base I retrieve data (with sql statements) which I convert into a pivot table. This proces I do in a vba programme in a constant flow (data from different suppliers of goods to our company).
The output is a number of Excel files (same number as suppliers).
However, each file contains a different range of data, ie number of lines.
Which VBA command can I use to tell the pivot table what the data range is???

If I do this in each file by hand I use (with my cursor in a data cell of the pivot table) "Options", and then "other data source", in order to catch all the basic data for the pivot table.

Please help!!

Thanks in advance from a cold Holland
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Thanks in advance from a cold Holland

Hello Hans,

You're welcome to come warm up with a visit to Sunny San Diego! :cool:

You can change the PivotTable SourceData with VBA using something like this...

Code:
Sub UpdateDataSource()
    Sheets("Pivots").PivotTables("PivotTable1").ChangePivotCache _
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=Sheets("MyData").Range("A1").CurrentRegion)
End Sub

As an alternative, you might want to explore using a Dynamic Named Range as your datasource.
 
Upvote 0
Hello JS,

Thank you very much for the solution. I got it working finally.
Some things I have altered to get it working.
Here is my code:

start of the code

Sheets("DATA").Select

GG = Range("A" & Rows.Count).End(xlUp).Row

Sheets("Draaitabel").Activate

ActiveSheet.PivotTables("Draaitabel1").ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=Sheets("DATA").Range("A5:F" & GG))

end of the code

For chilling down you are welcome in Holland!
Best regards, Hans
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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