|
- Sub tttt()
- 't = Timer
- Dim irow, j, k As Integer '只有k被定义成了integer,前面几个都是Variant
- k = 1
- irow = Range("a65536").End(xlUp).Row
- For j = 2 To irow
- Range("h" & j) = k '直接操作range对象数量大时速度会慢的比较明显
- If k = 1 Then '这个if结构可以优化一下
- a = 1
- ElseIf k = 20 Then
- a = -1
- End If
- k = k + a
- Next
- 't = Timer - t
- 'Debug.Print t
- End Sub
- Sub test()
- 't = Timer
- Dim irow As Long, j As Long, k As Long, a As Long
- Dim hrr() As Long
- k = 1
- a = -1
- irow = Range("a65536").End(xlUp).Row
- ReDim hrr(irow - 2, 0) '采用数组提速,这里下标是从0开始的
- For j = 0 To irow - 2
- hrr(j, 0) = k
- If k = 1 Or k = 20 Then a = -a
- k = k + a
- Next
- Range("h2").Resize(irow - 1, 1) = hrr
- 't = Timer - t
- 'Debug.Print t
- End Sub
复制代码 用数组写了一遍,思路是一样的,不过速度提上很明显,如果a列最后一行是10001行,我的电脑上运行时间如下:
sub tttt时间: 0.4400024
sub test时间:1.599121E-02 速度提升了 27.5152661993683 倍
|
|