Avatar of rfreud
rfreud
 asked on

Adding values in an array

I am relatively new at working with arrays.  I want to add the values in one array to the values in a second array.  Something like this

Dim array1() as double
Dim array2() as Double
Dim array3() as double

array1(0) = 2
array1(1) = 5

array2(0) = 1
array2(1) = 6

this is where the line I don't know how to write would go.

If I were to debug.print array3() I want to see the following

3
11

Thanks very much in advance.
Ron
Microsoft DevelopmentMicrosoft Access

Avatar of undefined
Last Comment
rfreud

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeffrey Coachman

This works fine for me as well also:
Dim array1(0 To 100) As Double
Dim array2(0 To 100) As Double
Dim array3(0 To 100) As Double

array1(0) = 2
array1(1) = 5

array2(0) = 1
array2(1) = 6

array3(0) = array1(0) + array2(0)
array3(1) = array1(1) + array2(1)

Debug.Print array3(0) & vbCrLf & array3(1)
rfreud

ASKER
I will give this a try tomorrow...  
Thanks,
Ron
Hamed Nasr

If upper bound for first array differs from the second:

Private Sub Command11_Click()
    Dim array1() As Double
    Dim array2() As Double
    Dim array3() As Double
   
    ReDim array1(3)
    ReDim array2(5)
    ReDim array3(5)
   
    Dim i As Integer
    For i = 0 To UBound(array1)
        array1(i) = i
        Debug.Print i
    Next
    For i = 0 To UBound(array2)
        array2(i) = i
        Debug.Print i
    Next
    If UBound(array1) >= UBound(array2) Then
        For i = 0 To UBound(array1)
            array3(i) = array1(i)
        Next
        For i = 0 To UBound(array2)
            array3(i) = array1(i) + array2(i)
        Next
    Else
        For i = 0 To UBound(array2)
            array3(i) = array2(i)
        Next

        For i = 0 To UBound(array1)
            array3(i) = array1(i) + array2(i)
        Next
    End If
    For i = 0 To UBound(array3)
        Debug.Print array3(i)
    Next
End Sub
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mike Tomlinson

Yeah...I put absolutely no error checking in my snippet; just assumed that all arrays were the same size and had the same lower/upper/bounds.
GRayL

And now you run into the age old problem of storing derivable data.  There you are with a1, a2, and a3.  Someone changes a value in a1 or a2, but does not update a3 which is the sum - now what?  You have to make sure every time you view the contents of a3, it is in fact re-created.
rfreud

ASKER
Thank you very very much, particularly for the additional comments.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.