1

概要:多くの行のデータをループして、ループのたびに異なる列のデータを約 6 つの異なる変数に格納しています。

質問:Range("A" & some iterator)を使用すると、かなりの量の CPU サイクルを節約できActiveCell.Offset(some number)ますか? 一方は他方よりどれくらい速いですか?

前もって感謝します!

4

1 に答える 1

7

まったくの退屈とちょっとした好奇心から、さまざまな方法で 100,000 個の Excel セルを循環させる粗雑なタイマーを作成しました。

理想的には、それぞれを何度も実行して平均を取る必要がありますが、速度の大まかな概要がわかります。

私のマシンでは、これは私が得るものです

ここに画像の説明を入力

Option Explicit

#If Win64 Then
    Public Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
#Else
    Public Declare Function GetTickCount Lib "kernel32" () As Long
#End If

Sub TestSpeedofReadingCells()
Dim tcStart As Long
Dim tcEnd As Long
Dim temp As Variant

Dim i As Long
Dim rngobject As Range
Dim rngItem As Range
Dim rngArray() As Variant
Const rowsToRead As Long = 100000


    '***Read individual cells using .Range
    With Sheet1
        tcStart = GetTickCount
        For i = 1 To rowsToRead
            temp = .Range("A" & i).Value
        Next i
        tcEnd = GetTickCount
        Debug.Print "Time1: " & tcEnd - tcStart
    End With

    '***Read individual cells using .Range & screenupdating off
    With Sheet1
        Application.ScreenUpdating = False
        tcStart = GetTickCount
        For i = 1 To rowsToRead
            temp = .Range("A" & i).Value
        Next i
        tcEnd = GetTickCount
        Application.ScreenUpdating = True
        Debug.Print "Time1a: " & tcEnd - tcStart
    End With

    '***Read individual cells using .Range & screenupdating off & using value2
    With Sheet1
        Application.ScreenUpdating = False
        tcStart = GetTickCount
        For i = 1 To rowsToRead
            temp = .Range("A" & i).Value2
        Next i
        tcEnd = GetTickCount
        Application.ScreenUpdating = True
        Debug.Print "Time1b: " & tcEnd - tcStart
    End With

    '***Read individual cells using range object
    With Sheet1
        Set rngobject = .Range("A1:A" & rowsToRead)
        tcStart = GetTickCount
        For Each rngItem In rngobject
            temp = rngItem
        Next rngItem
        tcEnd = GetTickCount
        Debug.Print "Time2: " & tcEnd - tcStart
    End With

    '***Read individual cells using activecell
    With Sheet1
        tcStart = GetTickCount
        .Range("A1").Select
        For i = 1 To rowsToRead
            temp = ActiveCell.Offset(i - 1, 0).Value
        Next i
        tcEnd = GetTickCount
        Debug.Print "Time3: " & tcEnd - tcStart
    End With

    '***Read individual cells using activecell & screenupdating off
    With Sheet1
        Application.ScreenUpdating = False
        tcStart = GetTickCount
        .Range("A1").Select
        For i = 1 To rowsToRead
            temp = ActiveCell.Offset(i - 1, 0).Value
        Next i
        tcEnd = GetTickCount
        Application.ScreenUpdating = True
        Debug.Print "Time3a: " & tcEnd - tcStart
    End With

    '***Read individual cells using array
    With Sheet1
        tcStart = GetTickCount
        rngArray = .Range("A1:A" & rowsToRead).Value
        For i = 1 To rowsToRead 'should really use Lbound to Ubound but only example
            temp = rngArray(i, 1)
        Next i
        tcEnd = GetTickCount
        Debug.Print "Time4: " & tcEnd - tcStart
    End With

End Sub
于 2012-08-29T20:50:05.660 に答える