Unable to create Pivot Table by VBA

srlakra

New Member
Joined
Mar 24, 2015
Messages
23
Hi,

I tried creating the pivot table by VBA code but I failed.

Can Anyone please help me???


Sub Pivot_Test()
Dim Psheet As Worksheet
Dim Dsheet As Worksheet
Dim Pcache As PivotCache
Dim Ptable As PivotTables
Dim Prange As Range
Dim Lastrow As Long
Dim Lastcol As Long

On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add before:=ActiveSheet
ActiveSheet.name = "PivotTable"
Application.DisplayAlerts = True
Set Psheet = Worksheets("PivotTable")
Set Dsheet = Worksheets("Data")
Lastrow = Dsheet.Cells(Rows.Count, 1).End(xlUp).Row
Lastcol = Dsheet.Cells(1, Columns.Count).End(xlLeft).Column
Set Prange = Dsheet.Cells(1, 1).Resize(Lastrow, Lastcol)
Set Pcache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Prange).CreatePivotTable(TableDestination:=Psheet.Cells(2, 2), tablename:="DemandPivotTable")
Set Ptable = Pcache.CreatePivotTable(TableDestination:=Psheet.Cells(1, 1), tablename:="DemandPivotTable")
With ActiveSheet.PivotTables("DemandPivotTable").PivotFields("UK")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("DemandPivotTable").PivotFields("Wk 22 ~ 25")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.name = "Wk 22 ~ 25"
End With
End Sub



S/L
Material
Prime
UK
201722(W)
201723(W)
201724(W)
201725(W)
Wk 22 ~ 25
AB
a1
as
ABas
4
4
AB
ae
ad
ABad
1
1
AB
fr
a
ABa
3
3
AB
hj
df
ABdf
3
3
AB
ki
t
ABt
1
3
2
1
7
AB
lo
g
ABg
2
2
AB
pp
h
ABh
2
1
3
AB
dv
ujj
ABujj
38
304
126
145
613
AB
df
ff
ABff
1
1
AB
se
fty
ABfty
1
1
2
AB
ss
jko
ABjko
1
1
2

<tbody>
</tbody>




Regards,
Satish
 

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)

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,470
Latest member
Subhash Chand

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