简体   繁体   English

另一种方法代替 do-until 循环使代码执行得更快

[英]Another way instead of do-until loop to make code execute faster

I need help on how to make my code execute faster.我需要有关如何使我的代码执行得更快的帮助。 I have a very large volume of data and my ' Do Until loop ' causes the slow execution.我的数据量非常大,我的“ Do Until loop ”导致执行缓慢。 I wonder if there's a way to make my code run faster.我想知道是否有办法让我的代码运行得更快。 I only used do until loop because I'm comfortable with it.我只使用 do until 循环,因为我对它很满意。 Thanks谢谢

Dim WB1 As Workbook
Dim ws1 As Worksheet
Dim last As Long
Dim i As Integer
Dim x As String, y As String

Set WB1 = ThisWorkbook
Set ws1 = WB1.Worksheets("Source")

last = ws1.Cells(Rows.Count, "X").End(xlUp).Row

i = 2
Do Until Sheets("Source").Cells(i, 1) = ""
    x = Left(Sheets("Source").Cells(i, 6), 3)
    y = Left(Sheets("Source").Cells(i, 23), 7)
    If x = "611" Or y = "INITIAL" Then

        Sheets("Source").Cells(i, 25) = "INITIAL"

    Else

        ws1.Range("Y2:Y" & last).Formula = "=VLOOKUP(A:A,'Assignment Reference'!$C:$E,3,FALSE)"

    End If

    i = i + 1
Loop

There are many possible way how to improve the performance of Excel's VBA code.有许多可能的方法可以提高 Excel 的 VBA 代码的性能。 At best you read some articles about that:充其量你读了一些关于这个的文章:


Edit: As suggested by Bond I here is what I do in my VBA code to improve the execution performance:编辑:正如邦德所建议的,我在这里是我在 VBA 代码中所做的以提高执行性能:

Option Explicit

Dim screenUpdating As Boolean
Dim calculation As XlCalculation
Dim enableEvents As Boolean
Dim displayPageBreaks As Boolean



' Freezes Excel into its current state to improve
' performance during executing macro code. Be sure
' to call DoEvents occasionally during execution to
' prevent completly freezing the Excel window.
' Call this routine before all other code but after
' setting up proper exception handling.
Public Sub freezeSystem()
    'Save Excel configuration to reset later
    screenUpdating = Application.screenUpdating
    calculation = Application.calculation
    enableEvents = Application.enableEvents
    displayPageBreaks = ActiveSheet.displayPageBreaks

    'Turn off some Excel functionality so your code runs faster
    Application.screenUpdating = False
    Application.calculation = xlCalculationManual
    Application.enableEvents = False
    ActiveSheet.displayPageBreaks = False 'Note this is a sheet-level setting, but only necessary for ActiveSheet if code do not change the ActiveSheet
End Sub




' Unfreezes Excel and resets it into the configuration it had
' before the freezeSystem() was executed.
' Call this routine at the end of the macro code and also during
' the cleanup of exception handling.
Public Sub defreezeSystem()
    ' Reset Excel configuration into previous state
    Application.screenUpdating = screenUpdating
    Application.calculation = calculation
    Application.enableEvents = enableEvents
    ActiveSheet.displayPageBreaks = displayPageBreaks 'Note this is a sheet-level setting, but only necessary for ActiveSheet if code do not change the ActiveSheet

    ' Perform recalculation of all formulas
    Application.CalculateFullRebuild
End Sub




' This routine is intended to update the Excel window
' while executing macro code in controlled manner if
' necessary. Be aware that calling this function too
' often will drastically reduce the execution performance.
Public Sub updateSystem()
    If Application.screenUpdating = False Then
        Application.screenUpdating = True
        Application.CalculateFullRebuild
        Application.screenUpdating = False
    Else
        Application.CalculateFullRebuild
    End If
End Sub

Call freezeSystem() at the beginning or your code to improve the performance during execution.在开头或您的代码调用freezeSystem()以提高执行期间的性能。 Call defreezeSystem() at the end of your code and while cleaning up during exception handling if necessary.在代码末尾调用defreezeSystem()并在必要时在异常处理期间进行清理。

I do it in this way:我是这样做的:

Sub entryPoint()
    On Error GoTo entryPointErrorHandler    ' set up exception handling
    freezeSystem
    [your regular code goes here]

entryPointCleanUp:                          ' clean up from exception and normal operation
    [your cleanup code goes here]
    defreezeSystem
    Exit Sub

entryPointErrorHandler:                     ' exception handling
    [your exception handling code goes here]
    GoTo entryPointCleanUp                  ' jump to clean up code

End Sub

One way to speed things up is to tell Excel not to bother with updating the screen or calculating cells.加快速度的一种方法是告诉 Excel 不要打扰更新屏幕或计算单元格。 Turn these off before the loop and turn them on after:在循环之前关闭这些并在之后打开它们:

'***** Before loop
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'***** After loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

Try using the references you already set.尝试使用您已经设置的参考。 Eg: instead of例如:代替

Do Until Sheets("Source").Cells(i, 1) = ""
x = Left(Sheets("Source").Cells(i, 6), 3)
y = Left(Sheets("Source").Cells(i, 23), 7)

use

Do Until Sheets("Source").Cells(i, 1) = ""
   x = Left(ws1.Cells(i, 6), 3)
   y = Left(ws1.Cells(i, 23), 7)

I would also try to avoid VLOOKUP , which is very slow.我也会尽量避免VLOOKUP ,这很慢。 If all you need is to check for the existence aa value in a range, COUNTIF is much faster.如果您只需要检查某个范围内是否存在 aa 值,那么COUNTIF会快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM