user3204098
user3204098

Reputation: 9

Sum Columns - VBA

I'm trying to sum each line in two columns, and reproduce the answer in a third column. Then I need to sum the next line in the column until the last line.

Eg. A1 + B1, A2 + B2, A3 + B3, etc.

So far I have the following

Sub Columns()
ActiveCell.Range("A1").Select
POS1 = Range("A3").Value
POS2 = Range("N3").Value
ActiveCell.Range("AA3").Select
While POS1 And POS2 <> ""
RES = POS1 + POS2
ActiveCell.Value = RES
ActiveCell.Offset(1,0).Select
Wend
End Sub

So far, I can do the sum of the first line, print it, then offset the answer cell. I can't for the life of me figure out how to offset the two variables. Could anyone help out?

On a side note, if I don't select Cell A1 in the workbook, all the calculations are considered using the active cell as the reference point, which messes everything up.

Thanks in advance!

Upvotes: 0

Views: 172

Answers (1)

Rory
Rory

Reputation: 34075

It appears you want something like this, which avoids looping cell by cell which will be relatively slow:

Sub Columns()
    Range("AA3:AA" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = "=A3+N3"
End Sub

Upvotes: 1

Related Questions