Fast way to add two array in VBA

C

Charles

It seems (see previous discussion) that the function "redim" is much
faster than going through a loop to reset an array to its initial
value.

I was wondering if there was a similar "smart" way to add two arrays
together, ie

doing something like

X=ZeSmartFunction(X,Y)

instead of

for i = 1 to n
X(i)=X(i)+Y(i)
next i
 
J

Jon Peltier

Redim sets the array's elements to blank. You don't want to set it to blank,
so you'll have to add the values together to generate new non-blank values.

- Jon
 
A

Alan Beban

Jon said:
Redim sets the array's elements to blank. . . .

No. If the array type is String(), it resets them to blank. But, e.g.,
if the array type is Boolean(), it resets them to False; and if the
array type is Integer() or Double(), iit resets them to 0.

Alan Beban
 
J

Jon Peltier

Duh, I was trying to differentiate between default values, which Alan has
clarified, and "initial" values, which may not be the default values,
depending on how the user has initialized the array. Unfortunately I rushed
through my response.

- Jon
 
C

Charles

Thanks. Actually I was mostly interested in a way to add two arrays. I
haven't found anything on google nor in VBA's help. I guess there is
no faster way to add two arrays together than to go through every
single element with a loop.

Charles
 
J

Jon Peltier

There might be easier ways, but not faster ways, if you have access to
functions that do the hard work in the function and you only see the
function call:

NewArray = SumArrays(Array1, Array2)

I'm sure there are slower ways.

- Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top