Estoy utilizando Excel como un banco de datos para lo cual realizo la
coneccion que les muestro adelante, pero cuando llego a la "asignacion" del
recordset al cache de la tabla dinamica (ya existente) me da el siguiente
error "Method Recordset of object Pivotcache failed". Me pueden decir como
corregirlo y si estoy haciendo bien la asignacion a la tabla dinamica ya
existente. Esto ultimo lo hice con una ayuda que Hector me dio hace unos años
pero no se si funciona en la actualidad. Uso Excel 2007. Nota: el
CopyFromRecordset lo utilice transitoriamente para saber si la consulta se
estaba ejecutando momentaneamente, aqui es un comentario.
Sub Prueba()
Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim PC As PivotCache
Dim PT As PivotTable
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data
Source=C:\Users\Owner\Documents\Rendimientos\Cartago.xls;" & _
"Extended Properties=Excel 8.0"
.Open
End With
Set rst = New ADODB.Recordset
With rst
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
End With
rst.Open "SELECT FECHA, HH, Media, SUM(SUM), Dia FROM
[VentasHora$A1:F15000]" & _
"GROUP BY FECHA, HH, Media, Dia", cn, , , adCmdText
' ThisWorkbook.Worksheets("Sheet4").Range("A2").CopyFromRecordset rst
Set PC = ActiveWorkbook.PivotCaches(4)
Set PC.Recordset = rst
Set PT = ActiveSheet.PivotTables("PivotTable2")
PT.PivotCache.Refresh
rst.Close
Set rst = Nothing
cn.Close
Set cn = Nothing
End Sub
Leer las respuestas