更快地计算500,000行

时间:2015-11-14 03:55:08

标签: excel vba

我需要您的帮助才能找到Excel vba代码。我输入行作为RNC名称和CellID。在Concatenate列中,我使用连接公式来组合RNC名称和CellID。这是通过一个记录的宏来完成复制/粘贴特殊的3-500,000个单元格所需的时间非常长。

RNC Name    CELLID  Concatenate     Cell Name   Count
RNCBESI05   10621   RNCBESI05_10621 ALJRMG_1    31
RNCBESI05   10621   RNCBESI05_10621 ALJRMG_1    31
RNCBESI05   10621   RNCBESI05_10621 ALJRMG_1    31

请您建议如何更快地计算1 / 2M细胞?

录制的代码:

Sheets("Neighbor").Select
Range("C2").Select
    ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],""_"",RC[-1])"
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Cell!C[-3]:C,4,0)"
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])"
    Range("C2:E2").Select
    Selection.Copy
    Range("B2").Select
    Selection.End(xlDown).Offset(0, 1).Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Selection.End(xlUp).Select

1 个答案:

答案 0 :(得分:2)

将单元格范围值捕获到变量数组并在内存中处理应该可以缩短计算时间。

Sub three_ops()
    Dim v As Long, vVALs As Variant, vCELs As Variant

    With Worksheets("Cell")
        Debug.Print Intersect(.Columns("A:D"), .UsedRange).Address(0, 0)
        vCELs = Intersect(.Columns("A:D"), .UsedRange).Value2
    End With

    With Worksheets("Neighbor")
        vVALs = Intersect(.Columns("A:D"), .UsedRange).Value2
        For v = LBound(vVALs, 1) + 1 To UBound(vVALs, 1)
            vVALs(v, 3) = Join(Array(vVALs(v, 1), vVALs(v, 2)), Chr(95))
            If Not IsError(Application.Match(vVALs(v, 3), Application.Index(vCELs, 0, 1), 0)) Then
                vVALs(v, 4) = Application.VLookup(vVALs(v, 3), vCELs, 4, 0)
            End If
        Next v
        With Intersect(.Columns("A:D"), .UsedRange)
            .Value = vVALs
            With .Resize(.Rows.Count - 1, 1).Offset(1, 4)
                .Formula = "=countif(" & .Offset(0, -1).Address(1, 0) & ", d2)"
                .Value = .Value2
            End With
        End With
    End With
End Sub

COUNTIF function和变体数组我从来没有取得多大成功。您可能需要查看Is there a faster CountIf以加快操作速度。