Adding Arrays...

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Adding Arrays...

Post by NYIntensity »

Good afternoon :D

I'm trying to add two arrays to create a third; all three are 3x3 arrays. Anyway, I get as far as

Dim Matrix1(2,2) As Double
Dim Matrix2(2,2) As Double
Dim Matrix3(2,2) As Double

And then kind of get confused. I want to add Matrix 1 + Matrix 2, and I know I need to step through each location incrementally in Matrix 1 and Matrix 2 (at the same time). I don't want the entire answer, just a healthy shove in the right direction.

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Adding Arrays...

Post by rory »

You need two For..Next loops, one inside the other.
Regards,
Rory

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Adding Arrays...

Post by NYIntensity »

Yep, I understand that, but have no clue how to do it yet

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Adding Arrays...

Post by rory »

Hard to say much more with it still just being a hint, but:
Myarray3(x, y) = myarray1(x, y) + myarray2(x, y)
Regards,
Rory

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Adding Arrays...

Post by NYIntensity »

Code: Select all

For intCount1=0 to 2
     For intCount2=0 to 2
           Matrix3(intCount1,intCount2)=Matrix1(intCount1,intCount2)+Matrix2(intCount1,intCount2)
     Next intCount2
Next intCount1

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Adding Arrays...

Post by NYIntensity »

:D

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Adding Arrays...

Post by agibsonsw »

NYIntensity wrote:

Code: Select all

For intCount1=0 to 2
     For intCount2=0 to 2
           Matrix3(intCount1,intCount2)=Matrix1(intCount1,intCount2)+Matrix2(intCount1,intCount2)
     Next intCount2
Next intCount1
Hello. I wanted to ask a very similar question.
Is it not possible to add two arrays without looping? Can I multiply each element of an array by a number
without looping?
I'm also trying to add a number to an Excel range, but the following yields an error:
Range("A1:A10") = Range("A1:A10") + 5
Thanks for any advice,
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

NYIntensity
Lounger
Posts: 47
Joined: 26 Jan 2010, 01:05

Re: Adding Arrays...

Post by NYIntensity »

I don't think so; your problem would be kind of easy though:

For intCount1=0 to 2
For intCount2=0 to 2
Range(intCount1,intCount2)=Range(intCount1,intCount2)+5
Next intCount2
Next intCount1

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Adding Arrays...

Post by agibsonsw »

That's a shame.
Why is the instruction
Range("A1:A4") = Range("A1:A4") + 5
a Type Mismatch?
Ta, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Adding Arrays...

Post by StuartR »

This is a type mismatch because you are trying to add together a number and a range. This doesn't make any sense to VBA.
StuartR


User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Adding Arrays...

Post by agibsonsw »

Thank you. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Adding Arrays...

Post by rory »

Ranges can be done easily enough, arrays not so:

Code: Select all

Range("A1:A10").Value = [A1:A10 + 5]
Regards,
Rory

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Adding Arrays...

Post by agibsonsw »

Wow! I've just picked up on your answer.
I've used [A1:A10] many times, but where did [A1:A10+5] come from? Why/how does this work?

(How did you discover this?) Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Adding Arrays...

Post by rory »

That notation is a shortcut for Evaluate. It's very useful for manipulating ranges without loops (and sometimes arrays). I can't recall where or when I first came across it, as with most things I know!
Regards,
Rory