I have a datasheet form that exports as an Excel file (through an embedded macro on a button click event). I then call a function (in that same macro) that opens the Excel and the file and applies conditional formatting and subtotaling to it.
This works fine except that after I close the Excel file, an Excel process is left stuck in my task manager, so when I try to run the click event again for another customer, Excel won't open but the system thinks I have it open and it thinks the file I closed out of is still open.
I have tried a couple of statements in my code to close or clear the Excel program but nothing is working. Very frustrating since the rest of the code works fine. Anyone know how to fix this?
Here's my code:
Function FormatDataSht()
Dim xlApp As Object
Dim strPath As String
Dim myMsg As String
strPath = "\\SYSPRO\Exports\SalesHistByItemPc.xlsx"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
With xlApp
Workbooks.Open FileName:=strPath
Columns("I:I").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Columns("M:M").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Columns("A:M").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(6, 7, 8, 10 _
, 11, 12), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End With
'Set xlApp = Nothing
xlApp.Application.ActiveWorkbook.Save
xlApp.Quit
End Function
This works fine except that after I close the Excel file, an Excel process is left stuck in my task manager, so when I try to run the click event again for another customer, Excel won't open but the system thinks I have it open and it thinks the file I closed out of is still open.
I have tried a couple of statements in my code to close or clear the Excel program but nothing is working. Very frustrating since the rest of the code works fine. Anyone know how to fix this?

Here's my code:
Function FormatDataSht()
Dim xlApp As Object
Dim strPath As String
Dim myMsg As String
strPath = "\\SYSPRO\Exports\SalesHistByItemPc.xlsx"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
With xlApp
Workbooks.Open FileName:=strPath
Columns("I:I").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Columns("M:M").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Columns("A:M").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(6, 7, 8, 10 _
, 11, 12), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End With
'Set xlApp = Nothing
xlApp.Application.ActiveWorkbook.Save
xlApp.Quit
End Function