I'm working in MS Access 2013 on Windows 10.
I'm trying to open an Excel file on disk, change the column formatting and some columns, save and quit.
The Sub runs, but after the .Quit
command, "EXCEL.EXE" keeps running and subsequent calls to that Sub will result in run-time errors.
If I close Access, "EXCEL.EXE" disappears on the Task Manager, but not if I do "Compact and Repair" the database.
I've another Sub that opens an Excel file on disk and alters the width of all columns to "auto-width", then closes, which doesn't leave "EXCEL.EXE" running.
I tried different line orders on Dims and Sets to Nothing, Workbook Closes, etc.
I searched here and on other sites. The only suggestions for VBA is to use something like ThisWorkbook.Saved = True
. I tried that before and after .Quit
to no effect.
Other than that, I only find solutions for VB.NET or other environments.
Sub changeXLcolumnFormatting()
Dim XL As Object
Set XL = CreateObject("Excel.Application")
XL.Visible = False
XL.DisplayAlerts = False
XL.Workbooks.Open "C:\Users\640344\Desktop\rawDataTest.XLSX"
Dim sht As Worksheet
With XL
Set sht = ActiveWorkbook.Worksheets(1)
Dim rng As Range
Dim i As Integer, j As Integer
field_names = Split("datasistema|Data de Registo|Data Registo CMVM", "|")
sht.Select
end_of_table = sht.UsedRange.Columns.Count
For j = 0 To UBound(field_names)
For i = 1 To end_of_table
Set rng = sht.Cells(1, i)
If InStr(rng.Text, field_names(j)) > 0 Then
sht.Columns(i).NumberFormat = "yyyy-mm-dd HH:MM:ss"
End If
Next i
Next j
End With
Set rng = Nothing
Set sht = Nothing
XL.ActiveWorkbook.Close (True)
XL.Quit
Set XL = Nothing
End Sub
Excel
running? – BremserApplication.Quit
in stead ofXL.Quit
just to see how it would work. – Bremser