VBA & Macro Problems

Close and Delete an SAP-Exported Excel File with VBA

Khadija KhanKhadija Khan Nov 14, 2026 869 views

Question details

The user needs to close and delete an XLSX file exported from SAP using an Excel VBA macro.

Close and Delete an SAP-Exported Excel File with VBA
Product
Microsoft Excel VBA and SAP
Device & OS
not provided
Scenario
A macro exports an XLSX file from SAP, copies its data to another workbook, and then attempts to delete the exported file.
Observed behavior
The Kill command fails to find or delete the exported workbook, potentially because the path is incorrect or the file is still in use.
Before you start

Confirm the exported file's actual name and location in File Explorer, and wait for SAP to finish exporting it. Make sure the data you need has been copied before closing or deleting the export.

Solution 1Recommended

Verify the Export Path and Close the Correct Workbook Before Deleting It

Use an explicit workbook variable and its full path to avoid deleting the wrong file or relying on whichever workbook is active.

1
Identify the exported workbook

In the Excel VBA editor, open the macro and assign the exported workbook to a variable. For example, use `Set exportedWb = Workbooks("SAP_Export.xlsx")`, replacing the name with the actual open workbook name. Declare `exportedWb` as `Workbook` and `exportPath` as `String`.

2
Check the full path

After the export has finished saving and the data has been copied, capture the path with `exportPath = exportedWb.FullName`. Add `MsgBox exportPath` before deletion and compare the displayed path and filename with the file in File Explorer.

3
Close and release the workbook

After confirming the path, use `exportedWb.Close SaveChanges:=False` followed by `Set exportedWb = Nothing`. This discards unsaved changes in the exported workbook, so save them first if they are needed.

4
Delete the export

After closing the workbook, use `Kill exportPath` to delete the verified file. Run the macro and check the export folder to confirm that the file is gone.

Verify the Export Path and Close the Correct Workbook Before Deleting It
If deletion still fails: Check whether the file is open in another Excel instance or still being used by SAP. Allow the export to finish, close the other instance or process using the file, and try again. Only quit an Excel application instance if the macro created and owns it.
Free Microsoft Office alternative

Review SAP-Exported Spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for opening and reviewing exported spreadsheets. Its familiar interface and Microsoft Office format support make it easy to move between office suites, although it does not fix Excel VBA or SAP automation errors.

Open and review SAP-exported spreadsheet data.Work with Microsoft Excel XLSX files.Use a familiar spreadsheet interface for an easier transition.Install a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why might ActiveWorkbook.Path point to the wrong folder?

ActiveWorkbook refers to the workbook currently in focus, which may not be the SAP export. Assign the exported workbook to a variable and use its FullName property to capture the complete path.

How can I see the filename passed to Kill?

Add `MsgBox exportPath` immediately before the Kill command, then compare the displayed path and filename with the exported file in File Explorer.

Why does Kill fail after I close the workbook?

The path may be incorrect, or another Excel instance, SAP process, or application may still be using the file. Verify the path and close the process holding the file before trying again.

Does closing the export with SaveChanges set to False save my edits?

No. `exportedWb.Close SaveChanges:=False` discards unsaved changes in that workbook. Save any needed changes before running the close command.