Option Compare Database
Option Explicit
Sub The()
ProcessTEST False
End Sub
Public Function ProcessTEST(Test As Boolean) As Boolean
' Past this function in MS Access 2010 code module
' in Tools Reference - set a reference to MS Excel 2010
' In Immediate Window - type in either ? ProcessTest(TRUE) or ? ProcessTEST(FASLE)
Dim ObjXL As Excel.Application
Dim objWB As Excel.Workbook
Dim objWS As Excel.Worksheet
Dim strNewReportPath As String ' for directory to save
Dim intWorksheetNum As Integer
Dim intRowNumber As Integer
Dim intColumnNumber As Integer
Dim intRowPos As Integer
Dim intHeaderColCount As Integer
Dim intMaxheaderColCount As Integer
Dim strSaveAsFileName As String ' the name with time stamp to save this report
Set ObjXL = CreateObject("Excel.Application")
ObjXL.Visible = True ' ******* change as needed
Set objWB = ObjXL.Workbooks.Add ' defaults to 3 sheets to the wind
strNewReportPath = "C:\" & Year(Now()) & "-" & Month(Now()) & "-" & Day(Now()) & "-" & Hour(Now()) & "-" & Minute(Now()) & "-" & "PivotTest.xlsx"
objWB.SaveAs FileName:=strNewReportPath
Set objWS = ObjXL.Sheets("Sheet1")
With objWS
.Range("A5").Value = "Dog"
.Range("B5").Value = "Cat"
.Range("C5").Value = "Owl"
.Range("D5").Value = "Mouse"
.Range("A6").Value = "1"
.Range("B6").Value = "2"
.Range("C6").Value = "3"
.Range("D6").Value = "4"
.Range("A7").Value = "9"
.Range("B7").Value = "8"
.Range("C7").Value = "7"
.Range("D7").Value = "6"
.Range("A8").Value = "3"
.Range("B8").Value = "4"
.Range("C8").Value = "5"
.Range("D8").Value = "6"
.Range("A9").Value = "7"
.Range("B9").Value = "6"
.Range("C9").Value = "5"
.Range("D9").Value = "4"
End With
ObjXL.DisplayAlerts = False
objWB.Names.Add Name:="Data1", RefersToR1C1Local:=objWS.Range("A5").CurrentRegion ' Set name rage Data1 to currentregion
Set objWS = objWB.Sheets("Sheet2")
objWB.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"=Data1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="Sheet2!R5C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
objWS.Name = "AveragePermitTime"
If Test Then
objWB.Close SaveChanges:=True
ObjXL.Quit
Set ObjXL = Nothing ' click Code RESET (stop code) - Excel.EXE goes away in Task Manager
If Err.Number = 0 Then ProcessTEST = True
Exit Function
' Excel.EXE is not in task manager everything closed as expected
End If
ObjXL.Sheets(2).PivotTables(1).Name = "AverageDays_Area"
'ObjXL.ActiveSheet.PivotTables(1).Name = "AverageDays_Area" ' <<--- Also Keeps Excel process if this is run
DoEvents ' Workbook visible - sure enough PivotTable Name is updated
' under PivotTable Tools - Options - then on menu PivoTable Name: see the new name AverageDays_Area is in fact updated
ObjXL.ActiveWorkbook.SaveAs FileName:=strNewReportPath
ObjXL.ActiveWorkbook.Close
'770 ObjXL.Visible = True
ObjXL.Quit ' Workbook closes
'ObjXL.Quit ' As suggested - Excel.EXE still runs
Set ObjXL = Nothing ' Click Code RESET (stop code) - Excel.EXE still in Task Manager
If Err.Number = 0 Then
ProcessTEST = True
' Excel.EXE is still running in Task Manager
End If
Exit Function
End Function