VBA Code Not Closing Excel Application

JohanGduToit

Board Regular
Joined
Nov 12, 2021
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Afternoon Experts,

I have an MS Access application that reformat an Excel spreadsheet that are imported into a MS Access table once reformatting has been completed. The only reformatting that takes place is a deletion of a worksheet named "Sum" and then deleting all 'empty' rows from the last cell in Column "A" that contains data to the end of the file. The code executes without any error; but the Excel Application remains open afterwards. So if I run the code again to format a 2nd Excel file, the program crash on line
VBA Code:
.Application.Rows(Range("A" & .Rows.Count).End(xlUp).Offset(1).Row & ":" & .Rows.Count).Delete

For some reason Excel do not close once code has been executed.

Please assist? I think this may have something to do with late/early binding; but I am not sure though.

Thank you.

VBA Code:
Public Sub FormatPargoInvoiceFile(sFile, sSheet As String)

'On Error GoTo Err_FormatPargoInvoiceFile

    Const xlUp As Long = -4162
    
    Dim xlApp As Object
    Dim xlSheet As Object
    
    Application.SetOption "Show Status Bar", True
    vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting & Processing Pargo Invoice File... Please wait.")
    
    Set xlApp = CreateObject("Excel.Application")
    Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets(1)
        
    With xlApp
        .Application.DisplayAlerts = False
        .Application.Sheets("Sum").Delete
        .Application.DisplayAlerts = True
        .Application.Sheets(sSheet).Select
        .Application.Rows(Range("A" & .Rows.Count).End(xlUp).Offset(1).Row & ":" & .Rows.Count).Delete
        .Application.Range("A1").Select
        .Application.ActiveWorkbook.Save
        .Application.ActiveWorkbook.Close
        .Application.Quit
    End With
    
    vStatusBar = SysCmd(acSysCmdClearStatus)
    
    Set xlSheet = Nothing
    Set xlApp = Nothing
    
'Exit_FormatPargoInvoiceFile:
'    Exit Sub
    
'Err_FormatPargoInvoiceFile:
'    vStatusBar = SysCmd(acSysCmdClearStatus)
'    MsgBox Err.Number & " - " & Err.Description
'    Set xlSheet = Nothing
'    Set xlApp = Nothing
'    Resume Exit_FormatPargoInvoiceFile
    
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try using
VBA Code:
.Quit
(instead of .Application.Quit)
 
Upvote 0
You missed a dot in front of Range:

VBA Code:
.Application.Rows(.Range("A" & .Rows.Count).End(xlUp).Offset(1).Row & ":" & .Rows.Count).Delete
 
Upvote 0
Solution
If xlApp is the application object you don't need Application as in
.Application.DisplayAlerts, so?

With xlApp
__.DisplayAlerts = False

Would it not be better to reference sheets at the workbook level rather than the application level? Reduces the chance that someday that reference will result in the wrong workbook being affected? Consider me an Excel vba novice.
 
Upvote 0
.Application.DisplayAlerts, so?
was supposed to be .Application.DisplayAlerts, no?
In other words, it's a question. Sorry for how that might have come across otherwise.
 
Upvote 0
Too late to edit now. Forgot to say I think the solution to the problem is to not close the workbook, just save then quit. However, I'd still edit the code along the lines of what I mentioned previously.
 
Upvote 0
You missed a dot in front of Range:

VBA Code:
.Application.Rows(.Range("A" & .Rows.Count).End(xlUp).Offset(1).Row & ":" & .Rows.Count).Delete
Hi Rory,

That was it!

Added the "." before "Range" ...solved!

Please see final working code below.

Thank you!
 
Upvote 0
If xlApp is the application object you don't need Application as in
.Application.DisplayAlerts, so?

With xlApp
__.DisplayAlerts = False

Would it not be better to reference sheets at the workbook level rather than the application level? Reduces the chance that someday that reference will result in the wrong workbook being affected? Consider me an Excel vba novice.
Hi Micron,

Applied your suggestion and removed ".Application" from various lines.

See working code below...

PS : Not sure what you mean by referencing sheets at the workbook level rather than the application level?

If you do have time, please elaborate and maybe amend my code accordingly?

Thank you once again.
 
Upvote 0
Final working code...

Hats off to all who replied; much appreciated:

VBA Code:
Public Sub FormatPargoInvoiceFile(sFile, sSheet As String)

On Error GoTo Err_FormatPargoInvoiceFile

    Const xlUp As Long = -4162
    
    Dim xlApp As Object
    Dim xlSheet As Object
    
    Application.SetOption "Show Status Bar", True
    vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting & Processing Pargo Invoice File... Please wait.")
    
    Set xlApp = CreateObject("Excel.Application")
    Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets(1)
        
    With xlApp
        .DisplayAlerts = False
        .Sheets("Sum").Delete
        .DisplayAlerts = True
        .Sheets(sSheet).Select
        .Rows(.Range("A" & .Rows.Count).End(xlUp).Offset(1).Row & ":" & .Rows.Count).Delete
        .Range("A1").Select
        .ActiveWorkbook.Save
        .Quit
    End With
    
    vStatusBar = SysCmd(acSysCmdClearStatus)
    
    Set xlSheet = Nothing
    Set xlApp = Nothing
    
Exit_FormatPargoInvoiceFile:
    Exit Sub
    
Err_FormatPargoInvoiceFile:
    vStatusBar = SysCmd(acSysCmdClearStatus)
    MsgBox Err.Number & " - " & Err.Description
    Set xlSheet = Nothing
    Set xlApp = Nothing
    Resume Exit_FormatPargoInvoiceFile
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top