Link to home
Start Free TrialLog in
Avatar of Lia Nungaray
Lia NungarayFlag for United States of America

asked on

How can I delete an open workbook using VBA?

I am using Access VBA to generate an Excel report. The excel workbook generated, but I ran into an error so I killed the process. I now have an excel workbook that I cannot delete because it is considered opened. How can I close/delete it?

Avatar of byundt
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

You can try the following steps to close and delete the Excel workbook:

  1. Close any open instances of Excel.
  2. Open Task Manager and look for any instances of Excel that may be running in the background. End those processes.
  3. Try to delete the Excel workbook again.

If the above steps don't work, you can try the following VBA code to force close and delete the workbook:

Sub CloseAndDeleteWorkbook()
    Dim wb As Workbook
    Set wb = Workbooks("WorkbookName.xlsx") 'Replace "WorkbookName.xlsx" with the name of your workbook
    wb.Close SaveChanges:=False
    Kill wb.FullName
End Sub

Open in new window

This code will close the workbook and then delete it using the Kill command. Make sure to replace "WorkbookName.xlsx" with the actual name of your workbook.