与VBAsortingloggingExcel损坏

我有一个用于过滤logging的macros的Excel文件。 运行macros后,我保存并closures文件。 一旦我再次打开文件,它说,该文件已损坏:

Excel found unreadable content in '[filename].xls'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes. 

一旦我点击是的文件打开,看着指引我的XML文件我find以下内容:

 <?xml version="1.0" encoding="UTF-8" standalone="true"?> -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <logFileName>error050360_01.xml</logFileName> <summary>Errors were detected in file 'C:\xxx\file.xlsb'</summary> -<removedRecords summary="Following is a list of removed records:"> <removedRecord>Removed Records: Sorting from /xl/worksheets/sheet11.bin part</removedRecord> </removedRecords> </recoveryLog> 

在Sheet11上,我有以下的sorting代码:

 LastRow = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.Count - 1, 0).End(xlUp).Row Range("A3").Select ActiveWorkbook.Worksheets("AP_PV").Sort.SortFields.Clear ActiveWorkbook.Worksheets("AP_PV").Sort.SortFields.Add key:=Range("A4:A" & LastRow) _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("AP_PV").Sort .SetRange Range("A3:B" & LastRow) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

我尝试在Range(“A3”)之前添加一个条件,selectLastRow> 4来排除sorting,但是id没有任何区别。

任何想法是什么导致腐败,如何避免它?

我会评论这个,只是提供链接,但我没有50声望呢。

Googlesearch在几年前发布了SE线程,并提供了大量可能的解决scheme: Excel错误:删除logging:从/xl/worksheets/sheet10.xml部分sorting

最有可能工作的是这一个:

 Sheets(yoursheetname).Sort.SortFields.Clear 

把你的sortingfunction(或在保存你的文件之前),似乎应该解决问题。

希望是有效的。