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.
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