我正在尝试对某些单元格进行排序,然后将我的列表中添加的标题加入中心并加粗,并且过程非常缓慢。这是一个工作宏的结束,我发现在添加下面的代码后它变慢了:
Columns("A:F").Select
ActiveWorkbook.Worksheets("search results").sort.SortFields.Clear
ActiveWorkbook.Worksheets("search results").sort.SortFields.Add Key:=Range( _
"E2:E112"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("search results").sort
.SetRange Range("A1:F112")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Worksheets("search results").Activate
Range("A1").EntireRow.Insert
Range("A1") = "A/A"
Range("B1") = "Tag"
Range("C1") = "Work"
Range("D1") = "Ôýðïò"
Range("E1") = "Date"
Range("F1") = "Remark"
Worksheets("search results").Range("A1:F1").Font.Bold = True
Worksheets("search results").Range("A1:F1").HorizontalAlignment =xlCenter
Worksheets("search results").Activate
Range("A1").Activate
有什么不对或有办法更有效吗?有些程序甚至没有响应,我必须关闭并重新启动。
答案 0 :(得分:0)
您可以尝试在脚本开头关闭屏幕更新,事件和计算,因为过滤器语句可能非常慢。 您可以查看Optimizing VBA macro了解如何选择脚本并提高性能的信息。
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Columns("A:F").Select
With ActiveWorkbook.Worksheets("search results")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range( _
"E2:E112"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
End with
With ActiveWorkbook.Worksheets("search results").Sort
.SetRange Range("A1:F112")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Worksheets("search results")
.Range("A1").EntireRow.Insert
.Range("A1") = "A/A"
.Range("B1") = "Tag"
.Range("C1") = "Work"
.Range("D1") = "Ôýðïò"
.Range("E1") = "Date"
.Range("F1") = "Remark"
.Range("A1:F1").Font.Bold = True
.Range("A1:F1").HorizontalAlignment = xlCenter
.Range("A1").Activate
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic