ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Fast way to add two array in VBA (https://www.excelbanter.com/excel-programming/396902-fast-way-add-two-array-vba.html)

Charles

Fast way to add two array in VBA
 
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


Jon Peltier

Fast way to add two array in VBA
 
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
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Charles" wrote in message
ups.com...
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




Alan Beban[_2_]

Fast way to add two array in VBA
 
Jon Peltier wrote:
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

Jon Peltier

Fast way to add two array in VBA
 
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
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Alan Beban" wrote in message
...
Jon Peltier wrote:
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




Charles

Fast way to add two array in VBA
 
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


Jon Peltier

Fast way to add two array in VBA
 
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
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Charles" wrote in message
oups.com...
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





All times are GMT +1. The time now is 01:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com