运行宏后Excel变得非常慢

时间:2014-09-22 12:57:05

标签: excel vba excel-vba excel-2010

我设计了一个宏来搜索"源工作簿"基于目标工作簿中的主要编号,找到匹配后,它将复制与之关联的整行(通常会为主编号复制5行),如果没有匹配,则会将该行涂成黄色。 /> 当我运行宏时,excel表变得非常慢,难以使用excel表操作 这是我的代码:

     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual
For i = 4 To 2000
If IsEmpty(Cells(i, 3)) Then
GoTo line

End If


Range(Cells(i, 3), Cells(i, 3)).Select
Selection.Copy
st = Cells(i, 3).Value
Windows(source).Activate



On Error GoTo error_handler

Cells.Find(What:=st, After:=ActiveCell, LookIn:= _
    xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveWindow.SmallScroll ToRight:=6

Range(ActiveCell.Offset(0, 10), ActiveCell.Offset(0, 12)).Select
Application.CutCopyMode = False
Selection.Copy
 Windows(target).Activate
 Range(Cells(i, 13), Cells(i, 13)).Select
 ActiveSheet.Paste
 Windows(source).Activate
 Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(1, 0)).Select
 var1 = ActiveCell.Row
var2 = var1
  For j = 1 To 50
     If IsEmpty(Cells(var2, 3)) Then
 var2 = var2 + 1
 Else
 Exit For
 End If
   Next j
  var2 = var2 - 1
  If var2 < var1 Then
  GoTo xy
   End If

  Rows(var1 & ":" & var2).Select



  Application.CutCopyMode = False
  Selection.Copy
  Windows(target).Activate
  Rows(i + 1 & ":" & i + 1).Select
  Selection.Insert Shift:=xlDown
  xy:
  line:


   error_handler:
    Windows(target).Activate


Resume label
label:
Windows(target).Activate
  Next i
 Application.Calculation = xlCalculationAutomatic
 Application.ScreenUpdating = True

请为我提供解决方案。 提前致谢

2 个答案:

答案 0 :(得分:1)

我尝试清除工作表中的条件格式,宏运行得更快。 谢谢大家参与解决问题

答案 1 :(得分:0)

当我进行单元格/范围操作时,我使用以下代码来加速Excel。将前三行放在SUB的开头,将最后三行放在sub&amp;的末尾。检查速度差异:

Sub SpeedUp()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub