Create a Pivot table

Orongo

Board Regular
Joined
Nov 6, 2011
Messages
83
Hi
I am working on creating a Pivot table using VBA, my data is

[TABLE="class: outer_border, width: 500"]
<tbody>[TR]
[TD]ID
[/TD]
[TD]Name[/TD]
[TD]bYear
[/TD]
[TD]Gender
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]Anna
[/TD]
[TD]1991
[/TD]
[TD]W
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]George
[/TD]
[TD]1981
[/TD]
[TD]M
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Sara
[/TD]
[TD]1971
[/TD]
[TD]W
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Tim
[/TD]
[TD]1961
[/TD]
[TD]M
[/TD]
[/TR]
</tbody>[/TABLE]

My code right now looks like
--------------------------------
Sub Create_Pivot_Table_From_Cache()

Dim oPC As PivotCache
Dim oPT As PivotTable
Dim oWS As Worksheet

oWS = ActiveSheet
oPC = ActiveWorkbook.PivotCaches.Create(xlDatabase, oWS.UsedRange)
oPT = oPC.CreatePivotTable(oWS.[D20], "Pivot From Cache", True)
oPT.AddFields(oPT.PivotFields("Item").Name, oPT.PivotFields("Customer").Name)
oPT.AddDataField(oPT.PivotFields("Qty"), "Quantity", xlSum)

End Sub
-------------------

But it doesn't do anything. How can the code be changed or written? I will expand this program to read in a large amount of data, so memory and speed is important. I hope for help as internet in this case is turning up bad results :(

(HELP!)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi all

after a lot of trial and error I managed to solve the issue. I hope this will help any future users :)

Sub Create_Pivot_Table_From_Cache()
Dim oPC As PivotCache
Dim oPT As PivotTable
Dim oWS As Worksheet

Set oWS = ActiveSheet
Set oPC = ActiveWorkbook.PivotCaches.Create(xlDatabase, Selection)
Set oPT = oPC.CreatePivotTable(oWS.[D20], "Pivot From Cache", True)
oPT.AddFields (oPT.PivotFields(2).Name), oPT.PivotFields(1).Name
oPT.AddDataField (oPT.PivotFields(3)), "Sum of Qty", xlCount

End Sub
 
Upvote 0

Forum statistics

Threads
1,225,155
Messages
6,183,218
Members
453,152
Latest member
ChrisMd

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