6

Working on a excel macros where I am trying to add the cell values from above cells to calculate total value. This is what my data looks likeSample Excel Document

Here I want to add above cell values for each column to calculate the sum. To accomplish this i have written a macro as follows.

For cl = 2 To 5    
    Worksheets(5).Cells(4, cl).Formula = "=SUM(B4:B6)"        
Next cl

This should set the formula to each cell in a row till 5 columns. But it sets the same formula on all cells in a row it should get change according to column. How to set sum formula for each cell for corresponding column ?

6 Answers 6

9

Not sure I quite understand your code. You seem to be writing into row 4, but you also want to sum from row 4 to row 6. That will create a circular reference.

Let's assume the formula is written into row 3 instead. You will want to use the R1C1 reference style to make the cells to sum relative to the current cells.

A trick to learn what reference to use is:

  • Get a new worksheet, enter =SUM(B4:B6) into cell B3 and copy to the right.
  • Then click File > Options > Formulas and select R1C1 Reference Style.
  • Now inspect the formulas in the sheet. You will see this: =SUM(R[1]C:R[3]C)

This is what you need in the macro.

For cl = 2 To 5    
    Worksheets(5).Cells(3, cl).FormulaR1C1 = "=SUM(R[1]C:R[3]C)"    
Next cl
Sign up to request clarification or add additional context in comments.

Comments

2

You can use very simple formula like below:

Sub sum_month()
Sheets("13").Activate
Range("D2").Formula = "=SUM(A1+A2+A3)"
End Sub

And next just hold and drag the cell to fill up another rows automatically.

Comments

2

For your case, you can use this:

For cl = 2 To 5
    ColName = Left(Cells(1, cl).Address(0, 0), (Cells(1, cl).Column < 27) + 2)
    ValueSum = "=SUM(" & ColName & "4:" & ColName & "6)"
    Worksheets(5).Cells(4, cl).Formula = ValueSum
Next

Comments

1

some more details to R1C1 and R[1]C[1] style in formulas. As far as I know R[1]C[1] creates a relative reference and R1C1 creates a absolute reference. But keep in mind that the figures for R[x] and C[y] are an offset to the cell which contains the formula.

That means if you want to show the sum of A1:B4 in C5 the code has to be like this:

Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R[-4]C[-2]:R[-1]C[-1])"   

If you want to the same but ending up with an absolute reference is aht to look like this.

 Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R1C1:R4C2)" 

Comments

0

Try something like this.

For cl = 2 To 5
    ColName = Left(Cells(1, cl).Address(False, False), 1 - (cl > 26))
    Worksheets(5).Cells(4, cl).Formula = "=SUM(" & ColName & "4:" & ColName & "6)"
Next cl

Comments

0

I think you can just reference the whole formula range and Excel will be smart enough to adjust the columns.

With no loop:

Worksheets(5).Cells(4, cl).resize(1,4).Formula = "=SUM(B4:B6)"

Comments

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.