Excel VBA For循环导致100%CPU

时间:2017-04-19 09:04:26

标签: excel-vba vba excel

Application.ScreenUpdating = False
Dim r As Range
Dim a As Long


Set op = Worksheets("ZVCTOSTATUS")
Set CP = op.Columns("J")
Set CTO = op.Range("J1")
Set OD = op.Columns("G")
Set ZV = op.Columns("H")

op.Activate
fa = op.Range("J" & Rows.Count).End(xlUp).Row
Set r = op.Range("J2:J" & fa)

For Each C In r
   CTO = CP.Cells(C.Row, 1).Value
    If CTO = "FG BOOKED" Or CTO = "CLOSED" Then
       ZV.Cells(C.Row, 1) = 0
    ElseIf CTO = "NOT STARTED" Or CTO = "UNCONFIRMED" Then
        ZV.Cells(C.Row, 1) = OD.Cells(C.Row, 1).Value
End If
Next C

大家好,我正在使用此代码通过我的工作表制作一个For循环,通过引用列J来更改H列中的值。

当在独立工作表上使用此代码时,它似乎工作得很好但是一旦我将它移植到一个更大的文件,它有数据连接,我只是单独运行这个宏,它导致我的CPU运行在100%并且需要最多10分钟。

有谁知道为什么会这样?

3 个答案:

答案 0 :(得分:2)

为了帮助您的宏运行更顺畅,您可以在主代码(正好在子代码下方)和代码之后(在结束子代码之前)插入以下代码 这将关闭屏幕更新,警报,并将计算设置为手动,因此在流程运行之前,不会更新任何公式。

   'Please Before Main Code'
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   Application.Calculation = xlManual

  'Insert main code here'

  'Place After Main code'
   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
   Application.Calculation = xlAutomatic

答案 1 :(得分:1)

看起来你陷入了一个具有以下特征的陷阱:

  1. 您正在使用大小为几MB的大型Excel文件
  2. excel文档中包含公式和数据连接
  3. 此外,它可能有透视表和图表
  4. 公式的计算选项为自动
  5. 试试这个: 1.转到公式选项卡 2.单击“计算选项” 3.选择“手动”

    Screenshot for your reference

    现在执行您创建的宏。去吧应该不错。一旦宏被执行。您可以更改计算选项。

    注意:您也可以使用以下代码段控制计算选项:

        Dim CalcMode As Long
        ' This will set the calculation mode to manual
        With Application
          CalcMode = .Calculation
          .Calculation = xlCalculationManual
          .ScreenUpdating = False
          .EnableEvents = False
        End With
    
         << Add your macro processing here >>
    
        ' Again switch back to the original calculation option
        With Application
           .ScreenUpdating = True
           .EnableEvents = True
           .Calculation = CalcMode
    End With
    

    每次更改任何单元格时,Excel都会尝试计算值(基于公式)。这是针对宏更新的每个单元格的整个文档完成的。因此,对于大型Excel文档,它会导致高CPU消耗。

答案 2 :(得分:1)

您正在设置一个单元格的值,一次触发重新计算。正确执行此操作的方法是首先将列读入内存,设置值并使用一个操作写入结果

Public Sub AnswerPost()

    Dim r_status As Range, r_value As Range, r_calc As Range
    Dim i As Long, n As Long
    Dim op As Worksheet

    Set op = Worksheets("ZVCTOSTATUS")
    ' Find the number of items on cell "J2" and below
    n = Range(op.Range("J2"), op.Range("J2").End(xlDown)).Rows.Count
    ' Set the n×1 range of cells under "J", "G" and "H" columns
    Set r_status = op.Range("J2").Resize(n, 1)
    Set r_value = op.Range("G2").Resize(n, 1)
    Set r_calc = op.Range("H2").Resize(n, 1)

    Dim x_status() As Variant, x_value() As Variant, x_calc() As Variant
    ' Read cells from the worksheet into memory arrays
    x_status = r_status.Value2
    x_value = r_value.Value2
    x_calc = r_status.Value2
    ' Set values of x_calc based on x_status, row by row.
    For i = 1 To n
        Select Case x_status(i, 1)
            Case "FG BOOKED", "CLOSED"
                x_calc(i, 1) = 0#
            Case "NOT STARTED", "UNCONFIRMED"
                x_calc(i, 1) = x_value(i, 1)
        End Select
    Next i
    ' Write the resulting array back into the worksheet
    r_calc.Value2 = x_calc
End Sub

上述代码的测试用例

screen