Close and Delete an SAP-Exported Excel File with VBA
Question details
The user needs to close and delete an XLSX file exported from SAP using an Excel VBA macro.

- 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.
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.
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.
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`.
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.
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.
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.

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.

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.




