How to optimize (or avoid as far as possible) loops in Excel VBA

时间:2016-02-12 21:42:40

标签: excel vba excel-vba

Various problems in Excel VBA involving selections (and how to avoid them) and "heavy" loops have been discussed on this forum earlier on. I'd like to expand on that a little. Let's say I want to change a certain string to another string and do something else with numeric data. Consider the following code:

SELECT 'conjugation' FROM 'normal_verbs' WHERE " . $y . " LIKE CONCAT('%',root,'%')

Now, this code works "perfectly" but is hardly an efficient one. Should I optimize it by: 1) first finding all cells that contain data in the first place, 2) loop only over those cells again, 3) divide the data now even further to numeric and non-numeric data, and 4) execute what I want on these numeric and non-numeric ranges? Or should I possibly combine some of these steps in one loop?

3 个答案:

答案 0 :(得分:2)

将整个范围变为变量(结果是包含2d数组的变量)会快几个数量级,循环变量数组进行更改,然后将变量数组返回到工作表。

答案 1 :(得分:2)

我已经做了一些与此相似的事情。

1°: 使用效果优于 整数 以进行高度反击。

2°:使用范围属性进行检查。

3°:使用应用程序属性来轻松进行计算。

您无需使用“For ... 0 to 3000”来执行此操作。你可以尝试这个:

Sub LoopExample()

Application.ScreenUpdating = False

Application.Calculation = xlCalculationManual

Application.EnableEvents = False

Dim i As Range

For Each i In Range("A:Z")

If IsEmpty(i.Value) = False Then

If i.Value = "Cat" Then

i.Value = "Dog"

ElseIf IsNumeric(i.Value) Then

If (i.Value) / 2 < 0.5 Then

i.Value = 0.5

Else

End If

End If

End If

Next


Application.EnableEvents = True

Application.Calculation = xlCalculationAutomatic

Application.ScreenUpdating = True

End Sub

但是,如果坚持使用计数器,请尝试这样的事情:

Sub numberlist()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

Dim a As Long
Dim b As Long

Dim i As Range

Do While Not Cells(3000, 3000).Value > 0

b = b + 1

Cells(1, b).Value = 1 + b * 100 - 100

Cells(2, b).Value = 2 + b * 100 - 100

Set i = Range(Cells(1, b), Cells(3000, b))

Set SourceRange = Range(Cells(1, b), Cells(2, b))

Set fillRange = i

SourceRange.AutoFill Destination:=fillRange

Loop


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

试驾。它只是花了很多时间来计算:v

答案 2 :(得分:1)

我认为你可以通过让Excel只查看带有值的单元格来优化这一点。为此,您可以将SpecialCells组合用于常量和公式 - 我认为两者的组合将捕获所有“填充”或非空白单元格:

Sub LoopExample()

  Dim ws As Worksheet
  Set ws = ActiveSheet
  Dim r, s As Range

  Set r = Application.Union(ws.UsedRange.SpecialCells(xlCellTypeConstants), _
      ws.UsedRange.SpecialCells(xlCellTypeFormulas))

  For Each s In r

    If s.Row >= 30 And s.Row <= 3000 And s.Column >= 30 And s.Column <= 3000 Then
      If s.Value2 = "Cat" Then
        s.Value2 = "Dog"
      End If
    End If
  Next s

End Sub

我没有列出你评估细胞的所有逻辑,但是你明白了。