Ale ainda estou quebrando a cabeça, pois queria deixar esse codigo o mais didático possível...
Desta forma peço que me ajude com o seguinte proposito.
1º Gostaria de criar separado um modolo de Conexão com o Banco (Access).
2º Montar a Pivot Table em um modulo separado... hoje o codigo estar assim :
Sub AleVBA_12873()
'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, fieldCount As Long
Dim rng As Range
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'--------------
'THE CONNECTION OBJECT
strDBName = "DATA_BASE_SALES_TESTE.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For Access 2007 (.accdb database) use the ACE Provider: "Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
'--------------
'OPEN RECORDSET, ACCESS RECORDS AND FIELDS
Dim ws As Worksheet
'set the worksheet:
Set ws = ActiveWorkbook.Sheets("DB_Access")
Application.ScreenUpdating = 1
ws.Cells.Clear 'limpa as células
Set adoRecSet = New ADODB.Recordset 'Set the ADO Recordset object:
strTable = "bd_dados" 'Opening the table named SalesManager:
adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic
'Daqui pra baixo eu inclui com o intuito de após a conexão já criar a tabela direto
Dim pt As PivotTable
Dim cacheOfpt As PivotCache
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next
Sheets("DB_Access").Select
ActiveSheet.PivotTable("MyPt").TableRange2.Clear
Set cacheOfpt = ActiveWorkbook.PivotCaches.Create(xlExternal, strDBName)
Sheets("DB_Access").Select
Set pt = ActiveSheet.PivotTables.Add(cacheOfpt, Range("a2"), "MyPT")
adoRecSet.Close
'close the objects
connDB.Close
'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
ws.Columns(4).EntireColumn.Delete
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = 1
End Sub
se puder colocar em cada linha o que deve ser incluso por favor.
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel
Postado : 01/09/2014 12:35 pm