Hello,
I have been having trouble creating a pivot table in Excel from an Access module. The basic process is I have a series of complex queries in Access that I first export to Excel worksheets (which will be the data source for the pivots). From here, I wish to automate the creation of a pivot table in Excel. I have automated plenty of pivot tables directly from Excel with no issue, but for this particular report the VBA and queries reside in Access and I am creating and naming a new workbook on the fly through VBA.
The issue seems to be with defining the SourceData range in the Pivot Cache. I receive a type mismatch with the pivot cache code snippet below. I have tried to fully qualify the range in a variety of ways, such as (after activating the worksheet in question) and nothing seems to work:
Worksheets("Paid Production Detail").Range("A1:T80000")
Here is the pivot cache code giving me issues. This code will actually create the pivot table container on the worksheet in question, but then it bombs out with the type mismatch.
--------------------------------------------------------------------
With wkb
Set wks = wkb.Worksheets.Add(After:=.Sheets(.Sheets.Count))
wks.Name = "Paid Pivot"
End With
Set PSheet = wkb.Sheets("Paid Pivot")
Set DSheet = wkb.Sheets("Paid Production Detail")
'Define Data Range
LastRow = DSheet.Cells(DSheet.Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, DSheet.Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache - type mismatch here
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="PaidPivotTable")
I have been having trouble creating a pivot table in Excel from an Access module. The basic process is I have a series of complex queries in Access that I first export to Excel worksheets (which will be the data source for the pivots). From here, I wish to automate the creation of a pivot table in Excel. I have automated plenty of pivot tables directly from Excel with no issue, but for this particular report the VBA and queries reside in Access and I am creating and naming a new workbook on the fly through VBA.
The issue seems to be with defining the SourceData range in the Pivot Cache. I receive a type mismatch with the pivot cache code snippet below. I have tried to fully qualify the range in a variety of ways, such as (after activating the worksheet in question) and nothing seems to work:
Worksheets("Paid Production Detail").Range("A1:T80000")
Here is the pivot cache code giving me issues. This code will actually create the pivot table container on the worksheet in question, but then it bombs out with the type mismatch.
--------------------------------------------------------------------
With wkb
Set wks = wkb.Worksheets.Add(After:=.Sheets(.Sheets.Count))
wks.Name = "Paid Pivot"
End With
Set PSheet = wkb.Sheets("Paid Pivot")
Set DSheet = wkb.Sheets("Paid Production Detail")
'Define Data Range
LastRow = DSheet.Cells(DSheet.Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, DSheet.Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache - type mismatch here
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="PaidPivotTable")