2

几个月来我一直在使用这个网站来帮助我开始我早期的编码生涯,这是我在这里的第一篇文章。如果我昨天在搜索论坛时错过了这个问题,我深表歉意。我有一个包含大约 6,000 个活动 Sumif 的电子表格,它们引用了一个包含 500,000 多行的表。可以想象,这需要一些时间来计算。我已将它们写入 VBA,因此它们仅在用户选择这样做时进行计算。但是,由于 sumif 如此之多,代码运行大约需要 3-5 分钟。我正在寻找加快速度以获得更好的最终用户体验的方法。我将在下面发布我如何执行 sumifs。仅在某些情况下,这是对同一组用户执行 sumifs 两次,但使用一个不同的标准。如果我遗漏了任何相关信息,请告诉我。

For i = 1 To 12
    Range("Emp_Utility").Offset(1, i).Activate
    Do Until Cells(ActiveCell.Row, 2) = ""
        ActiveCell.Value = Application.WorksheetFunction.SumIfs(Hrs, Emp, Cells(ActiveCell.Row, 2), Y, Cells(4, ActiveCell.Column), M, Cells(3, ActiveCell.Column))
        ActiveCell.Offset(1, 0).Activate
    Loop
Next i

For a = 1 To 12
    Range("Emp_Billable").Offset(1, a).Activate
    Do Until Cells(ActiveCell.Row, 30) = ""
        ActiveCell.Value = Application.WorksheetFunction.SumIfs(Hrs, Emp, Cells(ActiveCell.Row, 2), Y, Cells(4, ActiveCell.Column), M, Cells(3, ActiveCell.Column), Bill, "No")
        ActiveCell.Offset(1, 0).Activate
    Loop
Next a
4

3 回答 3

2

将范围加载到变量数组中,然后在代码中编写 SUMIFS,而不是使用公式。如果您需要示例,请告诉我,我将指导您完成。

编辑:没有问题。这是一个例子。

Sub example()

    Dim EmpUtil, EmpBillable As Variant   ' Creates variant array

    EmpUtil = Range("Emp_Utility")        'Places Range into EmpUtil Array
    EmpBillable = Range("Emp_Billable")   'Places Range into EmpBillable Array

    For x = LBound(EmpUtil) To UBound(EmpUtil)   'Cycles through EmpUtil Rows
        'Do comparisons and additions here
        'References to arrays should be something like
        ' "EmpUtil(x,3) = example" - for the 3rd column

        'for calculations on each column you cycle through each column for that row
        For y = LBound(EmpUtil, 2) To UBound(EmpUtil, 2)
            EmpUtil(x, y) = Calculation

        Next y


    Next x

    For x = LBound(EmpBillable) To UBound(EmpBillable)   'Cycles through EmpBillable Rows
        'Do comparisons and additions here


    Next x

    Range("Emp_Utility") = EmpUtil         'Places EmpUtil Array back into Range
    Range("Emp_Billable") = EmpBillable    'Places EmpBillable Array back into Range


End Sub

这应该让你开始。

于 2013-10-03T15:04:52.120 回答
1

You might consider an alternative to SUMIFS (which can take forever to calculate) such as a combination of SUM and SORT. Please see the answer to How to perform SumIf using VBA on an array in Excel which looks at using a SUM and SORT combination resulting in very fast calculations whilst still returning the same values as if a SUMIFS method had been used.

于 2014-02-02T23:58:12.643 回答
0

我不建议使用数组匹配方法来处理excel中的大数据(客户信息、贷款卡)。您最好在代码开始时使用该 sumif 关闭自动更新,然后再打开它们。遵循以下命令:希望这会有所帮助!

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual

<Your sumifs code>

ActiveSheet.Calculate
Application.ScreenUpdating = True
Application.DisplayAlerts = True
于 2021-09-23T05:14:09.827 回答