1

IS there a quick way to do this?

For example I have two rows

A B

34 5
56 6
34 3
25 2

I want to do the following calculation down the rows.

A1 = A1+B1
A2 = A2+B2
A3 = A3+B3 ..
..

Now I can do this with a macro that loops though the rows like

for x = 1 to 500
sheet1.cells(x,1).vlaue = sheet1.cells(x,1).vlaue + sheet1.cells(x,2).vlaue
next x

but it there a more efficient way by using ranges or something where it can be carried out as a single step?

Cheers

2
  • is there a specific reason why you want to overwrite the first coloum with the result? Commented Jul 29, 2012 at 0:10
  • yes. the first collum is a current value, and the second is the step value. So each time a user clicks a button to evulate the sheet. some totals are updated and the step values are added to the "current values" to give the new "current values". Yes I could do it with three coloums, current, step, and current+step. But I would prefer not to. Commented Jul 29, 2012 at 0:15

3 Answers 3

3

A quick way is to enter from the immediate window:

[a1:a500]=[a1:a500+b1:b500]

The square brackets are a shortcut for the Evaluate function

Sign up to request clarification or add additional context in comments.

1 Comment

This is about perfect for what I want. Very nice and simple.
1

I can think of two options:

Option 1

Use Copy, Paste Special, Add

Sub AddStep()
    Dim rng1 As Range, rng2 As Range

    Set rng1 = Range([A1], [A1].End(xlDown))
    Set rng2 = rng1.Offset(, 1)
    rng2.Copy
    rng1.PasteSpecial xlPasteValues, xlPasteSpecialOperationAdd
    Application.CutCopyMode = False

End Sub

Option 2

Loop over a variant array. This is much faster than looping over a range

Sub AddStep()
    Dim rng As Range
    Dim dat As Variant
    Dim i As Long

    Set rng = Range([B1], [A1].End(xlDown))
    dat = rng
    For i = LBound(dat, 1) To UBound(dat, 1)
        dat(i, 1) = dat(i, 1) + dat(i, 2)
    Next
    rng = dat
End Sub

Option 1 is faster, but in some circumstances it is best to avoid the clipboard.
Tested on a sample of 1,000,000 rows:
Option 1 - 435ms
Option 2 - 2589ms

Comments

0

The way I would do it is create a formula for the first row, then just select all the rows after and press Ctrl+D. Isn't this what you are after? Unless I misunderstood the question.

1 Comment

you cant do a formula in cell A1 of = A1+B1, this would be a cyclic formula where you are using the destination cell as an argument for the calculation. You have to use some VB code to keep it tidy. Trouble with using loops to loop though the rows is it is not efficient, some programming languages allow you preform calculations on arrays in a much more efficient manor. Wondering if this can be done in excel to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.