Range和Cells这两个函数,都可以获取单元格内容和将内容写入单元格。既然这两个功能相同,那么速度就成为了选择的关键。
注:Range有着比Cells更强大的内容,大家千万不要把这两个函数完全等同。当然了,这也从侧面反应了速度上必然Range更慢。
Sub test()
Dim i As Long
Dim a As String
For i = 1 To 100000 Step 1
Cells(i, 1) = CStr(i)
Next i
QueryPerformanceCounter m_Start
For i = 1 To 100000 Step 1
' 下面四句中选一句执行
a = Range("A" & CStr(i)) 'Range read
a = Cells(i, 1) 'Cells read
Cells(i, 1) = a 'Cells write
Range("A" & CStr(i)) = a 'Range write
Next i
QueryPerformanceCounter m_Now
Elapsed = 1000 * (m_Now - m_Start) / m_Frequency
Debug.Print Elapsed
End Sub
测试结果的时间单位是毫秒。
第几次测试 | Range read | Cells read | Range write | Cells write |
---|---|---|---|---|
1 | 1019.9720 | 344.1759 | 6220.0159 | 5105.3813 |
2 | 1020.0355 | 346.0292 | 6216.2530 | 5103.4524 |
3 | 1020.8382 | 345.4371 | 6207.0230 | 5079.1922 |
4 | 1023.1144 | 344.1790 | 6198.1607 | 5090.5974 |
5 | 1018.8067 | 344.7482 | 6188.6159 | 5091.1590 |
6 | 1024.7189 | 344.8035 | 6181.9272 | 5085.4331 |
7 | 1017.7546 | 342.1849 | 6183.4536 | 5100.7955 |
8 | 1023.6095 | 344.1097 | 6188.9005 | 5074.8064 |
9 | 1015.5275 | 344.2437 | 6186.2355 | 5076.1584 |
10 | 1021.1321 | 343.7072 | 6187.4848 | 5083.3936 |
原文:https://www.cnblogs.com/dreamofjay/p/12489311.html