View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Fastest way to reset to zero an array

Do you mean a VB array, as suggested by the subject line, or a range of
cells as implied when you say "going through every single cell"

If a VB array you can ReDim the array or use the Erase function. Both
methods will reset the array without need to iterate each element.

However with large arrays that you will want to use again before they go out
of scope, surprisingly it can be faster to loop through the array resetting
default value, "" with string arrays, 0 with number arrays or Empty if a
Variant array.

If you are talking about worksheet cells, to enter 'zero' or any other value
into multiple cells simply
rng.Value = 0 ' same as Ctrl-Enter

or clear / clearcontents

Regards,
Peter T


"Charles" wrote in message
oups.com...
Hello

This is a relatively simple question but I can't find it on google
group. What is the fastest way to reset an array to zero (or the value
by default). (in this case, performance is key, every micro-second
counts!)

Intuitively, I would say that going through every single cell and
setting it to zero must be under-efficient, as VBA has to interpret a
let more instructions that if there is a function that already does
that. I was thinking to "redim". But zero-ing an array is not redim's
main purpose. Is there a better way?

Thanks
Charles