I have two columns A and B with numbers as values.
- In C1 I want
=A1 + B1 - In C2 I want
=A2 + B2
and so on. I have written the following VBA code – while it works it adds “0” after the end of the last row in range.
Let’s assume my last row is A10. It adds “0” in C11 when I run the code.
How do I prevent this?
Sub macro()
Dim R As Long
R = 1
Do
Cells(R, "C").Select
R = R + 1
ActiveCell.Formula = "=sum(" & ActiveCell.Offset(0, -2) & "," &
ActiveCell.Offset(0, -1) & ")"
Loop Until IsEmpty(ActiveCell.Offset(0, -2))
End Sub
Just replace your Until condition to the following string:
Loop Until IsEmpty(ActiveCell.Offset(1, -2))That will check the right cell for being empty. The rest of your code should remain intact.