Wauvel

Excel like a finance pro.

← The library

VBA · Performance

Processing in arrays

Read a range into an array, loop in memory, write it back once; far faster than cell-by-cell loops.

CommonDifficulty 1400 · Advanced
Practice · 2 questions →

When to use it

Read a range into a Variant array, loop in memory, and write the whole array back once. Each cell access is a slow call into Excel; an array loop is pure VBA and hundreds of times faster.

The code

Code
v = rng.Value
For i = 1 To UBound(v, 1): v(i, 2) = v(i, 1) * 2: Next
rng.Value = v

Worked examples

  • Read, loop, write

    v = rng.Value For i = 1 To UBound(v, 1): v(i, 2) = v(i, 1) * 2: Next rng.Value = v Column 2 becomes double column 1, in one read and one write

    The pattern.

  • Scan a column

    v = Range("A2:A10000").Value For i = 1 To UBound(v, 1) If v(i, 1) < 0 Then n = n + 1 Next Counts negatives in 10,000 rows instantly

    Read-only scans.

  • Output array

    ReDim out(1 To UBound(v, 1), 1 To 1) ... Range("B2").Resize(UBound(out, 1), 1).Value = out A separate output array written to a resized target

    Build results separately.

Worth knowing

  • Arrays from ranges are always 2D and 1-based.
  • Resize the target to match before writing.
  • Use Value2 to skip date and currency conversion.

Where it goes wrong

  • Writing a 1D array to a column writes only the first element unless transposed.
  • Errors in cells come through as Error variants; test with IsError.

Related

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.