I have an Excel file with a macro that filters records. After running the macro I save and close the file. Once I open the file again it says that the file has been corrupte
I'd comment this, simply providing the link, but I don't have 50 reputation yet.
A Google search turned up an SE thread from a few years ago with plenty of possible solutions: Excel Error: Removed Records: Sorting from /xl/worksheets/sheet10.xml part
Most likely to work is this one:
Sheets(yoursheetname).Sort.SortFields.Clear
Put that after your sort function (or before saving your file), and it seems the issue should be resolved.
Hopefully that works.
The error tells it is "sheet11" and you could copy the content to a new workbook, delete the sheet in the error work book and create a new sheet with the content
I was having the same issue in Office 365 Excel for Windows. I was applying the sort to a worksheet object, but I failed to deallocate the object at the end of the sub. Once I added the set ws = nothing code to the end of the sub, the problem stopped occurring. I also cleared the sort fields as suggested earlier, but only after adding the deallocation code did this stop entirely.
Deallocation is always good practice. We often hear that the objects are dropped when they go out of scope, but that is not always the case.