Excel 2007 returns `Application-defined or object-defined error`
I have Excel 2007 and Windows XP When this code runs:
Columns("A:G").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B20000") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("开发者_JS百科A2:A20000") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F2:F20000") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:G20000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.Calculation = xlCalculationAutomatic
It throws an error at .Apply
:
Application-defined or object-defined error
One thing to note is that Application.Calculation = xlCalculationManual
Also, I cannot do anything in the UI except for switching tabs and opening the office menu, and I have to go to the Task Manager and click end task to exit, whereupon it asks if I want to save. Pressing cancel does not fix it. If I hit "no", it just closes. If I hit yes, it calculates and then asks if I want to have it recover my work.
I moved Application.Calculation = xlCalculationAutomatic
to right before it and that fixed it.
Application.Calculation = xlCalculationAutomatic
Columns("A:G").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B20000") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A20000") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F2:F20000") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:G20000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Edit: Oops, it doesn't after all.
Well, it turned out that it doesn't happen unless the computer is loaded with running programs. I did a pretty good rewrite of it and it is much faster now. The problem seems to be taken care of.
精彩评论