Sub TAB_EXP()
On Error GoTo TAB_EXP_err
Dim oRst As Object
Set oRst = CreateObject("New ADODB.Recordset")
oRst.CursorLocation = oRst.adUseClient
oRst.Open "Select DATLAV, Order, DA, ID, CODPRP, Colli From Q_Voice_fascia_Month;", CurrentProject.Connection
Dim oApp, oWks As Object
Set oApp = CreateObject("Excel.Application")
Set oWks = CreateObject("Excel.Workbook")
Set oWks = oApp.Workbooks.Add()
oApp.Visible = True
oWks.Sheets(1).Name = "Dati"
With oWks.Sheets("Dati").QueryTables.Add(oRst, oWks.ActiveSheet.Range("A1"))
.Name = "QryEstra"
.FieldNames = True
.BackgroundQuery = True
.RefreshStyle = oWks.xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Set oRst = Nothing
Dim oRng As Object
Set oRng = CreateObject("Excel.Range")
Set oRng = oWks.Application.Range("QryEstra")
With oWks.Sheets(2)
.Name = "TabellaPivot"
.Activate
End With
oWks.PivotTableWizard xlDatabase, oRng, oWks.Sheets("tabellaPivot").Cells(1, 1), "MyPivot", False, False
With oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("DATLAV")
.Orientation = oWks.xlColumnField
.Position = 1
End With
With oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("Order")
.Orientation = oWks.xlRowField
.Position = 1
End With
With oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("DA")
.Orientation = oWks.xlRowField
.Position = 2
.NumberFormat = "hh:mm:ss"
End With
With oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("ID")
.Orientation = oWks.xlRowField
.Position = 3
.NumberFormat = "hh:mm:ss"
End With
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").AddDataField oWks.ActiveSheet.PivotTables( _
"MyPivot").PivotFields("Colli"), "TotColli", oWks.xlSum
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").AddDataField oWks.ActiveSheet.PivotTables( _
"MyPivot").PivotFields("CODPRP"), "NrVoice", oWks.xlCount
oWks.Sheets("tabellaPivot").Range("D2").Select
With oWks.Sheets("tabellaPivot").PivotTables("MyPivot").DataPivotField
.Orientation = oWks.xlColumnField
.Position = 2
End With
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotSelect "Order[All;Total]", _
oWks.xlDataAndLabel, True
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("Order").Subtotals = Array(False _
, False, False, False, False, False, False, False, False, False, False, False)
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotSelect "DA[All;Total]", oWks.xlDataAndLabel _
, True
oWks.Sheets("tabellaPivot").PivotTables("MyPivot").PivotFields("DA").Subtotals = Array(False, _
False, False, False, False, False, False, False, False, False, False, False)
Dim stDocName1 As String
Dim stDocName2 As String
Dim stDocName3 As String
Dim stDocName4 As String
Dim stDocName5 As String
stDocName1 = Format(Forms![HomePage]![Lb_DateProdAdd], "yyyy/mm/dd")
stDocName2 = "H:\Comune\Dashboard\export\ProdBGA_"
stDocName3 = MonthName(Month(stDocName1), True)
stDocName4 = "_" & Year(stDocName1)
stDocName5 = stDocName2 & stDocName3 & stDocName4
oWks.Author = "fcarboni"
oWks.SaveAs FileName:=stDocName5
oWks.Close True
Set oRng = Nothing
Set oWks = Nothing
Set oApp = Nothing
Exit Sub
TAB_EXP_err:
Set oRng = Nothing
Set oWks = Nothing
Set oApp = Nothing
MsgBox Err.Number & " " & Application.AccessError(Err.Number)
End Sub