繁体   English   中英

使用许多ElseIf语句优化VBA速度

[英]Optimize VBA Speed with many ElseIf Statements

我正在VBA中运行一个代码,该代码花费的时间太长了。 它需要检查一个单元格中的值,确定该值是什么,如果该值被接受,则最多将4个对应的计数器中的1加1。 他们使我工作的方式归结为以下几点。

     Sub Overview

        Application.ScreenUpdating = False 'turns off screen updating to increase macro running speed
        Application.Calculation = xlCalculationManual 'Turns off automatic cell calculation to increase macro running speed

        Dim CountCondition1, SACountCondition1, WBDACountCondition1, StatusCountCondition1 As Long         'Defines counters for condition 1
        Dim CountCondition2, SACountCondition2, WBDACountCondition2, StatusCountCondition2 As Long         'Defines counters for condition 1
        'And so on for all 64 different values

        Dim UnitOverview As Range   'Defines range to use loop
        Dim Dataget As Long         'Defines counter to walk through loop

        ThisWorkbook.Sheets("Datadump").Range("E23").Value = Application.WorksheetFunction.CountA _
        (ThisWorkbook.Sheets("Status").Range("C:C")) - 1 
        'Displays total data amount in file, numbering about 132000 cells in column C

        For Each UnitOverview In ThisWorkbook.Sheets("Status").Range _
        ("A2:A" & ThisWorkbook.Sheets("Datadump").Range("E23").Value + 1) 
        'Create loop to check completion status for entire document. Column A contains the cells which have any of the 64 predefined values, these values are text based

        DataGetCompletion = (DataGetCompletion + 1)

        ThisWorkbook.Sheets("Datadump").Range("E17").Value = DataGetCompletion + 1 
        'Used to move cell reading range for each new loop cycle

        'The following code section counts the number of cells related to each plant
                If UnitOverview.Value = "Condition1" Then         
                   CountCondition1 = CountCondition + 1
                ElseIf UnitOverview.Value = "Condition2" Then
                   CountCondition2 = CountCondition2 + 1
        'This continues for all 64 different variables

        'The following section of code checks SA Terms for each value
        If UnitOverview.Value = "Condition1" And ThisWorkbook.Sheets("Status").Range _
        ("D" & ThisWorkbook.Sheets("Datadump").Range("E17").Value).Value = "Yes" Then
        SACondition1 = SACountCondition1 + 1
        ElseIf UnitOverview.Value = "Condition2" And ThisWorkbook.Sheets("Status").Range
        ("D" & ThisWorkbook.Sheets("Datadump").Range("E17").Value).Value = "Yes" Then
        SACountCondition2 = SACountCondition + 1
        ' Again this continues for all 64 different variables
        ' The same is then being done for the "WBDACOuntCondtion" terms and the "StatusCountCondition" Terms. 

        Next UnitOverview

        'Finally after the loop completes, all the different counters are assigend to cells in order to read them out and do whatever you like with 'em.

        Application.ScreenUpdating = True 'Turns screen updating back on
        Application.Calculation = xlCalculationAutomatic 'Turns automatic cell calculations back on

        End Sub

在A列的所有132000个单元上运行此代码大约需要15分钟,这对我来说很长。 我这样做是因为这是我知道的唯一方法。 任何帮助将非常感激。 我曾考虑过要创建一个列表来检查变量,但不会如何正确设置它,或者它是否会比我当前的解决方案更快。

我建议使用Excel COUNTIF函数,而不要在VBA中进行计算。 例如,要计算包含“ Condition1”的A2:A30范围中的单元格数量,请添加一个新的具有以下公式的单元格:

=COUNTIF(A2:A30,"Condition1")

在填充电子表格时将计算此值,因此您只需从VBA中的单元格中检索值即可。

您似乎有64 * 4个已明确定义的计数器-即256个明确定义的变量。 我将从重新考虑开始,而是有4个数组,每个数组包含64个元素,每个元素都是计数器值。

Dim CountCondition(1 to 64) As Long
Dim SACountCondition(1 to 64) As Long
Dim WBDACountCondition(1 to 64) As Long
Dim StatusCountCondition(1 to 64) As Long

(如果您愿意,可以使用0到63为数组建立索引-这只是一个示例。)

然后,只需评估这4种情况并增加数组的适当元素即可。

您也可以在循环中重复昂贵的范围评估,即使它们相同:

代替:

ThisWorkbook.Sheets("Status").Range("D" & ThisWorkbook.Sheets("Datadump").Range("E17").Value).Value = "Yes"

...在每次测试中,请将条件(true / false)提取到循环外部/循环之前的变量中:

Dim status_D_is_yes As Boolean
status_D_is_yes = ThisWorkbook.Sheets("Status").Range("D" & ThisWorkbook.Sheets("Datadump").Range("E17").Value).Value = "Yes"

...然后在检查中使用该布尔变量。

话虽如此,如果您提出第一个建议,那么第二个可能最终就显得多余了。 我会先做第二个建议(对不起!),因为它更容易执行,并且会立即提高性能。

暂无
暂无

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

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