- Use a For Each..Next loop, using a Variant to retrieve each value in turn.
- Use a For...Next loop, looping from the Lower Bound to the Upper Bound of the Array.
Expand|Select|Wrap|Line Numbers
- For Each varValue In alngValues
- j = varValue
- Next varValue
Expand|Select|Wrap|Line Numbers
- For lngCount = LBound(alngValues) To UBound(alngValues)
- j = alngValues(lngCount)
- Next lngCount
Expand|Select|Wrap|Line Numbers
- 'First, the API Declaration
- Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Expand|Select|Wrap|Line Numbers
- Public Function fForEach_Next(lngRepeats As Long) As Long
- 'Slower version of code using For Each...Next to process Array Elements
- Dim alngValues(1 To 1000) As Long
- Dim varValue As Variant, i As Long, j As Long
- Dim lngStartTime As Long
- For i = 1 To 1000
- alngValues(i) = i
- Next i
- lngStartTime = timeGetTime()
- For i = 1 To lngRepeats
- For Each varValue In alngValues
- j = varValue
- Next varValue
- Next i
- fForEach_Next = (timeGetTime() - lngStartTime)
- End Function
Expand|Select|Wrap|Line Numbers
- Public Function fFor_Next(lngRepeats As Long) As Long
- 'Faster version of code using For...Next to process Array Elements
- Dim alngValues(1 To 1000) As Long
- Dim lngCount As Long, i As Long, j As Long, T As Long
- Dim lngStartTime As Long
- For i = 1 To 1000
- alngValues(i) = i
- Next i
- lngStartTime = timeGetTime()
- For i = 1 To lngRepeats
- For lngCount = LBound(alngValues) To UBound(alngValues)
- j = alngValues(lngCount)
- Next lngCount
- Next i
- fFor_Next = (timeGetTime() - lngStartTime)
- End Function