Source Data For Pivot Table - VBA

parry

MrExcel MVP
Joined
Aug 20, 2002
Messages
3,355
Hi, we have a spreadsheet where information is entered each month into Sheet2. A Pivot table is located on Sheet1 and I want to update the Pivot table with the new range thats in Sheet2.

It seems I need to use the SourceData propoerty of the PivotCache but I get a type mismatch error. Any clues?

Code:
Sheets(1).PivotTables(1).PivotCache.SourceData = Sheets(2).Range("A5:T40000")
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi mate,

From the help file it appears as though the SourceData property needs to be specified as a cell reference rather than a range object. I tried this first of all:-

Sheets(1).PivotTables(1).PivotCache.SourceData = "Sheet1!A5:T40000"

but that produced an error. I then tried:-

Sheets(1).PivotTables(1).PivotCache.SourceData = "Sheet1!R5C1:R40000C20"

and that worked. I ended up using this code (meaning that you can avoid hard-coding the reference):-

Code:
    Sheets(1).PivotTables(1).PivotCache.SourceData = _
            Sheets(2).Name & "!" & Range("A5:T40000").Address(ReferenceStyle:=xlR1C1)

Any good?

Dan
 
Upvote 0
Good thinking Dan, however Im getting a 1004 Application-defined or object-defined error now.

I noticed that the sheet name was enclosed in single quotes. My latest effort is this with the same error...
Code:
Sheets(1).PivotTables(1).PivotCache.SourceData = "'" & Sheets(2).Name & "'!" & Range("A5:T40000").Address(ReferenceStyle:=xlR1C1)

Bloody pivot tables. Anymore ideas?
 
Upvote 0
Strange! Your code works OK for me in my sample workbook. What is the sheet name of your data source? Also, are there headings in every cell of the range A5:T5?

Dan
 
Upvote 0
For some reason its working now, although I didnt change the code (just closed and re-opened the workbook). Maybe my threat to throw the PC out the window from the 8th floor did the trick. :)

Thanks Dan (y)
 
Upvote 0
Sheets(1).PivotTables(1).PivotCache.SourceData = "'" & Sheets(2).Name & "'!" & Range("A5:T40000").Address(ReferenceStyle:=xlR1C1)

This will make range reference R1c1 style

Try below it worked for me.
Sheets(1).PivotTables(1).PivotCache.SourceData = "'" & Sheets(2).Name & "'!" & Range("A5:T40000").Address
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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