ActiveCell.Offset()和Range()之间的速度差异?

时间:2012-08-29 17:35:19

标签: excel excel-vba vba

摘要:我循环遍历多行数据,每次通过循环将来自不同列的数据存储在大约6个不同的变量中

问题:我会使用Range("A" & some iterator)而不是ActiveCell.Offset(some number)来节省大量的CPU周期吗?一个比另一个快多少?

提前致谢!

1 个答案:

答案 0 :(得分:7)

出于纯粹的无聊和一点点好奇心,我整理了一个粗略的计时器,用于循环100,000个Excel单元格的各种方法。

理想情况下,你应该多次运行并取平均值,但它会给你一个粗略的速度轮廓。

在我的机器上,这就是我得到的

enter image description here

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