creating a pivot in VBA without the data being formatted as a table?

monsierexcel

New Member
Joined
Nov 19, 2018
Messages
29
Hello dear friends, i am so thankful of the support i have had from the forum recently. i have recently got into VBA and have produced a pivot table from 2 data sources

However both data sources will only produce this pivot if the data thats copied in is formatted as a table (because of headers i assume) does anyone know if its possible to do?

thank you.

Code:
'Insert a New Blank Worksheet
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("Data3")

'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(1, 1), _
TableName:="SalesPivotTable")

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

'Insert Row Fields
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Ord No")
.Orientation = xlRowField
.Position = 1
End With
'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Date")
'.Orientation = xlRowField
'.Position = 2
'End With

'Insert Column Fields
'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Inc Vat")
'.Orientation = xlColumnField
'.Position = 1
'End With

'Insert Data Field
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Inc VAT")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "0.00"
.Name = "Revenue "
End With

'Format Pivot Table
ActiveSheet.PivotTables("SalesPivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("SalesPivotTable").TableStyle2 = "PivotStyleMedium9"

'Create Totals Sheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Totals").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Totals"
Application.DisplayAlerts = True

'sums totals
    'ActiveCell.Value = WorksheetFunction.Sum(Worksheets("Data4").Range("C2:C500"))

'copy paste AP pivot
'Dim ws As Worksheet

'Set ws = Worksheets.Add

'Sheets("PivotTable").Select
'ActiveSheet.PivotTables("AP Data").PivotSelect "", xlDataAndLabel, True
'Selection.Copy
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,214,800
Messages
6,121,641
Members
449,044
Latest member
hherna01

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