In diesem Beispiel wird ein neuer PivotTable-Cache erstellt, der auf einem OLAP-Anbieter basiert, und anschließend in Zelle "A1" im ersten Tabellenblatt ein neuer PivotTable-Bericht erstellt, der auf dem Cache basiert.
Set objPC = _
ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
With objPC
.Connection = _
"OLEDB;Provider=MSOLAP;Location=srvdata;Initial Catalog=National"
.MaintainConnection = True
End With
Set objPT = Worksheets(1).PivotTables _
.Add(PivotCache:=objPC, _
TableDestination:=Range("A1"))
In diesem Beispiel wird mit Hilfe einer ADO-Verbindung zu Microsoft Jet ein neuer PivotTable-Cache erstellt und dann in Zelle "A3" im aktiven Tabellenblatt ein neuer PivotTable-Bericht, der auf dem Cache basiert.
Dim cnnConn As ADODB.Connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command
' Open the connection.
Set cnnConn = New ADODB.Connection
With cnnConn
.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0"
.Open "C:\perfdate\record.mdb"
End With
' Set the command text.
Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = cnnConn
With cmdCommand
.CommandText = "Select Speed, Pressure, Time From DynoRun"
.CommandType = adCmdText
.Execute
End With
' Open the recordset.
Set rstRecordset = New ADODB.Recordset
Set rstRecordset.ActiveConnection = cnnConn
rstRecordset.Open cmdCommand
' Create a PivotTable cache and report.
Set objPivotCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlExternal)
Set objPivotCache.Recordset = rstRecordset
ActiveSheet.PivotTables.Add _
PivotCache:=objPivotCache, _
TableDestination:=Range("A3"), _
TableName:="Performance"
With ActiveSheet.PivotTables("Performance")
.SmallGrid = False
With .PivotFields("Pressure")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Speed")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Time")
.Orientation = xlDataField
.Position = 1
End With
End With
' Close the connections and clean up.
cnnConn.Close
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Set cnnConn = Nothing