2017-03-24 198 views
0

我有一個Excel文件與篩選記錄的宏。運行宏後,我保存並關閉文件。當我再次打開該文件時,它說,該文件已被損壞:Excel損壞與VBA排序記錄

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文件:

<?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我有以下排序代碼:

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 

我嘗試添加範圍(「A3」)之前的狀態選擇,看看是否LASTROW> 4,排除排序,但ID沒有任何區別。

任何想法是什麼導致腐敗,以及如何避免它?

回答

1

我會評論這個,只是提供鏈接,但我沒有50聲望呢。

谷歌搜索變成了一個SE線從幾年前用大量的可能的解決方案:Excel Error: Removed Records: Sorting from /xl/worksheets/sheet10.xml part

最有可能的工作是這樣的一個:

Sheets(yoursheetname).Sort.SortFields.Clear 

把你的排序功能後(或者在保存文件之前),並且似乎應該解決問題。

希望是有效的。