Darrell
Registered User.
- Local time
- Today, 22:34
- Joined
- Feb 1, 2001
- Messages
- 323
I have a function that pretty much does this, hopefully it might help you if you can modify it to your needs
Code:
Function Output_Fact_Hrs_Excel(FPAth As String)
On Error GoTo ErrorHandler
Dim fName As String
Dim strSheet As String
Dim MyFile As String
Dim appExcel As Object
Dim MyBook As Object
Dim MySheet As Object
fName = "Factory Hours.xls" ' Data file to overwrite
MyFile = FPAth & fName ' Point to the Data file
' Create the data file
Set appExcel = GetObject(, "Excel.Application")
DoCmd.OutputTo acOutputQuery, "qry Conf Hrs ~ Wk_Center", acFormatXLS, MyFile, False, ""
Set MyBook = appExcel.Workbooks.Open(MyFile)
Set MySheet = MyBook.Sheets(1)
' Modify the data file
With appExcel
Dim PV1Sheet As Object
Dim LoopCtr, FinalRow As Long
.ScreenUpdating = False
With MySheet
.Range("A1") = "Work Center"
.Range("B1") = "Conf. Date"
.Range("C1") = "Act. Lab"
.Range("D1") = "OT Lab"
.Range("E1") = "Total Lab"
.Rows(1).Font.Bold = True
.Cells.EntireColumn.AutoFit
FinalRow = .UsedRange.Rows.Count
MyBook.Sheets.Add After:=MySheet
End With
Set PV1Sheet = MyBook.Sheets(2)
With PV1Sheet
.Activate
.Name = "Totals"
MyBook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"qry Conf Hrs ~ Wk_Center!R1C1:R" & FinalRow & "C5").CreatePivotTable TableDestination:=.Range("A1"), TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
.PivotTableWizard TableDestination:=.Cells(3, 1)
.Cells(3, 1).Select
With PV1Sheet.PivotTables("PivotTable1")
.PivotFields("Work Center").Orientation = xlRowField
.AddDataField .PivotFields("Act. Lab"), "Sum of Act. Lab", xlSum
.AddDataField .PivotFields("OT Lab"), "Sum of OT Lab", xlSum
.AddDataField .PivotFields("Total Lab"), "Sum of Total Lab", xlSum
.DataPivotField.Orientation = xlColumnField
End With
End With
appExcel.ActiveWindow.ScrollWorkbookTabs Sheets:=-1
appExcel.ActiveWindow.TabRatio = 0.195
MyBook.Save
End With
ErrorHandlerExit:
appExcel.ScreenUpdating = True
Set appExcel = Nothing
Set PV1Sheet = Nothing
Set MyBook = Nothing
Set MySheet = Nothing
Exit Function
ErrorHandler:
If Err = 429 Then ' Excel is not running; open Excel with CreateObject
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Function
Last edited: