VBA macro to create pivot table on sheet2 with data source on sheet1

Josu

New Member
Joined
Mar 2, 2021
Messages
39
Office Version
  1. 2010
Platform
  1. Windows
I apologize, tried to write shortest possible header

Basically I have sheet calls Capped where I got my sql query exported (Columns A:K)
Based on that sheet Data I want to create a pivot table
I found one macro, but it doesnt work by some reason
From sheet Data I need 3 columns.
Customer = as row
Type = as column
Amount = as units

P.S. Found my issue, this due to incorrect range :( In sheet data I need to have cell A1 blank, how I need to edit my coordinates?
VBA Code:
Sub InsertPivotTable()

Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long

'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Total Capped Volume").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Total Capped"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Total Capped")
Set DSheet = Worksheets("Data")

'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Total Capped")

'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Total Capped")

'Insert Row Fields
With ActiveSheet.PivotTables("Total Capped").PivotFields("Customer")
.Orientation = xlRowField
.Position = 1
End With


'Insert Column Fields
With ActiveSheet.PivotTables("Total Capped").PivotFields("Type")
.Orientation = xlColumnField
.Position = 1
End With

'Insert Data Field
With ActiveSheet.PivotTables("Total Capped").PivotFields("Amount")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Amount "
Thank you in advance
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Can you write in
VBA Code:
Sheets("Sheet_Name").Range("A1").ClearContents
 
Upvote 0
Can you write in
VBA Code:
Sheets("Sheet_Name").Range("A1").ClearContents
Probably I explained incorrectly :(
I want to create pivot table which range starts in A1, but my macro doesn't work as A1 is blank cell (A2 and other cells under are occupied). Basically If i delete column A - everything works
 
Upvote 0
This clears the first Column.
VBA Code:
Sheets("Sheet_Name").Columns(1).ClearContents
 
Upvote 0
Sorry, I thought you said that if you deleted the first column everything works fine. Let me think on this one.
 
Upvote 0
Change your PRange value.
I would use the CurrentRange starting from (3,3) or C3.
 
Upvote 0
Change your PRange value.
I would use the CurrentRange starting from (3,3) or C3.
Not really working, when I change
VBA Code:
Set PRange = DSheet.Cells(3, 3).Resize(LastRow, LastCol)
nothing happening
 
Upvote 0
And I don't think you need to Set this value. Try
VBA Code:
PRange = DSheet.Range("C3").CurrentRegion
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,084
Members
453,021
Latest member
Justyna P

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