VBA pivot table code excel 2010 vs 2016

MrBoutte

New Member
Joined
Aug 17, 2016
Messages
2
I successfully made a macros in excel 2016, for creating a pivot table but I need to replicate the code for excel 2010. Copying over the code from the 2016 version into the 2010 doesn't work and I've also tried re-writing the code, to no avail. I don't understand what I'm doing wrong.
Here's the excel 2016 version that works perfect
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">[COLOR=#303336]Sheets[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Add
ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotCaches[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Create[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]SourceType[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]xlDatabase[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] SourceData[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"Superbills!R1C1:R563C86"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] Version[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]6[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]CreatePivotTable TableDestination[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"Sheet1!R3C1"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] TableName[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] DefaultVersion[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]6[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Provider Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Full Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]AddDataField ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Entry Date"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#7D2727]"Count of Entry Date"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] xlCount
ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Offset[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]ShowDetail _
    [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]False[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Name [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"Pivot Table"[/COLOR]</code>

now here's my adaption for the 2010 version, that isn't working

Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">[COLOR=#101094]Dim[/COLOR][COLOR=#303336] PTCache [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] PivotCache
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] PT [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] PivotTable

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] PTCache [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotCaches[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Create[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]SourceType[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]xlDatabase[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        SourceData[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]Range [/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Superbills!R1C1:R563C86"[/COLOR][COLOR=#303336]))[/COLOR][COLOR=#303336]

Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] PT [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Add[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]PivotCache[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]PTCache[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        TableDestination[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1!R3C1"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#303336] TableName[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]ShowPivotTableFieldList [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]True[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Provider Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Full Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]AddDataField ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336] _
        [/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Entry Date"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#7D2727]"Count of Entry Date"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] xlCount
    ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Offset[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
    ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]ShowDetail _
        [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]False[/COLOR][COLOR=#303336]
    Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
    Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Name [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"Pivot Table"[/COLOR]</code>
here's the compiler errors I'm getting:

"Name argument not found" highlighting
(Version:=6) in PTCache variable.

once that's removed.
I get the following
"Run-time error '1004': Method 'Range' of object '_Global' failed"
highlighting the PTCache variable again.
Code:
[INDENT]Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=Range("Superbills!R1C1:R563C86"))
[/INDENT]
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Use SourceData as a string:
Code:
SourceData:="Superbills!R1C1:R563C86"
 
Upvote 0
your suggesting to make SourceData a variable and just injecting that into the ActiveWorkbook.PivotCaches.Create parameters ?
 
Upvote 0
No, I'm saying that this:
Code:
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=Range("Superbills!R1C1:R563C86"))
should be this:
Code:
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Superbills!R1C1:R563C86")
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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