I am trying to ultimately create a pivot table that is bound to a
spreadsheet. Previously I had the pivot table bound to a recordset but I
wanted to append data. The way I thought I could get around this would be
to populate a datasheet with data and then append data when I needed to and
have the pivot table bound to the datasheet.
Here is my sample code to import the recordset
Private Sub BuildDataSheet(rs As ADODB.Recordset, sheet As Worksheet)
Dim TargetRange As Range
Dim intColIndex As Integer
Set TargetRange = sheet.Range("A1")
' optional approach for Excel 2000 or later (RS2WS is not necessary)
For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value =
rs.Fields(intColIndex).Name
Next
TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data
End Sub
and here is my code to bind the pivot table. Problem is that the last line
does not work properly , any ideas?
Private Function AddPivotTable(rst As ADODB.Recordset, sheet As Worksheet,
Datasheet As Worksheet) As PivotTable
'Dim pCache As Excel.PivotCache
Dim pTable As PivotTable
'Set pCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
'Set pCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase)
'Set pCache.Recordset = rst
'pCache.SourceData = Datasheet.Range("A1:FB4000")
'Set pTable = pCache.CreatePivotTable(sheet.Range("A8"))
'Set AddPivotTable = pTable
Set pTable = sheet.PivotTableWizard(xlConsolidation,
Datasheet.Range("A1", "F4000"), "A8")
End Function