Unable to Close Workbook VBA (Excel 97-2003)

JohanGduToit

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

I am struggling to Save and Close an Excel 97-2003 workbook after making some modifications to the file using VBA from MS Access (365). The execution seems to fail (hangs) on
the .Application.ActiveWorkbook.Close line and the workbook remain open until I manual reopen and save the file manually afterwards.

1646986558351.png


The code works without any issues when modifying a later version (i.e. .xlsx) of the same workbook, so I believe it's version (97-2003 .xls) related. Please advise as to how I can resolve this. Code below.


VBA Code:
Public Sub FormatCrazy(sFile As String)

'On Error GoTo Err_FormatCrazy

Dim xlApp As Object

Dim xlSheet As Object

Application.SetOption "Show Status Bar", True

vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting Crazy Stores File... Please wait.")


Set xlApp = CreateObject("Excel.Application")

Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets(1)


With xlApp

.Application.DisplayAlerts = False

.Application.Sheets(1).Select

.Application.Rows("1:1").Select

.Application.Selection.Delete shift:=xlUp

.Application.Range("C:C,E:E,G:G,H:H,J:J,K:K,M:M,N:N,O:O,P:P,Q:Q,R:R,S:S").Select

.Application.Selection.Delete shift:=xlToLeft

.Application.Range("A1").Select

.Application.ActiveWorkbook.Save

.Application.DisplayAlerts = True



.Application.ActiveWorkbook.Close



.Quit

End With



vStatusBar = SysCmd(acSysCmdClearStatus)



Set xlSheet = Nothing

Set xlApp = Nothing



'Exit_FormatCrazy:

' Exit Sub



'Err_FormatCrazy:

' vStatusBar = SysCmd(acSysCmdClearStatus)

' MsgBox Err.Number & " - " & Err.Description

' Set xlSheet = Nothing

' Set xlApp = Nothing

' Resume Exit_FormatCrazy



End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If you are able to verify that the previous .Application.ActiveWorkbook.Save has saved the spreadsheet, try changing the close line to:
.Application.ActiveWorkbook.Close SaveChanges:=False
 
Upvote 0
I believe I have managed to solve the problem by replacing:

.Application.ActiveWorkbook.Save

with

.Application.ActiveWorkbook.SaveAs sPath, FileFormat:=xlWorkbookNormal

where sPath = full path and filename of formatted Excel 97-2003 file
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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