Incrementing values in an array with a macro

hornsfan01

New Member
Joined
Jul 4, 2012
Messages
7
I'd like to have a macro that takes values in two separate columns of the same size and sums the values in each row and then replaces one of the columns with the new sums.​

So what I'd like to start with is this:

AB
1420
2210
3330
4510
5020
640

<tbody>
</tbody>

and run a macro that adds the values in A1 and A2 and puts the value in A2 then adds the values in B1 and B2 and puts that value in B2 and so forth.
I mention values because while B1:B6 will just be integers, A1:A6 will be formulas obtaining values from another part of the sheet and these values may change every time the sheet is recalculated. I don't want the formulas copied, just the values that the formulas end up with.

So I'd like to run the macro and end up with this:

AB
1424
2212
3333
4515
5020
644

<tbody>
</tbody>

I just started poking around in VBA today and tried to come up with something. I set A1:A6 as rCurrentValues and B1:B6 as rCumulativeValues after defining both as variables of type Range.

But unfortunately, while rCumulativeValues.Value = rCurrentValues.Value seems to work if I want to replace one with another, rCumulativeValues.Value = rCumulativeValues.Value + rCurrentValues.Value does not (but it explains what I'm conceptually trying to do).

Can I do this with a statement along the lines of above or do I need to set up a for each loop or something like that? If I need a loop, can someone write out this example for me, as I have been trying to learn VBA loops from various websites and many seem to infer some basic degree of understanding of VBA loop syntax, of which I have none.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this vba code

Code:
Sub SumMyArrays()
Dim i As Integer
'loop from the beginning row to the end row
For i = 2 To 7
   'Range(Cell Location).Value returns the value of the cell or assigns value to a cell
   Range("C" & i).Value = Range("C" & i).Value + Range("B" & i).Value
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,964
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top