2

I have the following code:

TotalCount = Application.Sum(Worksheets("Data").Range("B14:G14"))
Sheets("Data").Range("H14") = TotalCount
TotalCount = Application.Sum(Worksheets("Data").Range("B13:G13"))
Sheets("Data").Range("H13") = TotalCount
TotalCount = Application.Sum(Worksheets("Data").Range("B12:G12"))
Sheets("Data").Range("H12") = TotalCount
TotalCount = Application.Sum(Worksheets("Data").Range("B11:G11"))
Sheets("Data").Range("H11") = TotalCount

As you can see... it's not very efficient. Problem is, i cant find a loop solution to make it better. When i try to create a loop i either get results i dont expect or it doesnt work. I'm at the point where i need some help... help! And thank you in advance.

4
  • Maybe if you can include an example of what you tried, somebody can point out what you are doing wrong. Commented May 21, 2015 at 15:39
  • I tried something like this... but no joy: For i = 3 To 11 TotalCount = Application.Sum(Worksheets("Data").Range("Z" & i: "AE" & i) Sheets("Data").Range("AF" & i) = TotalCount Next i Commented May 21, 2015 at 15:50
  • Then i thought i'd use 'Cells' instead of 'Range'... but i'm not sure how that'd hang together. Commented May 21, 2015 at 15:52
  • Use a loop to achieve this Commented May 21, 2015 at 16:21

6 Answers 6

1

or, just for fun:

Dim i as integer
for i = 14 to 11 step -1
  Sheets("Data").Range("H" & i).Formula = "=SUM(B" & i & ":G" & i)
next
Sign up to request clarification or add additional context in comments.

Comments

0
For i = 1 to 4
    For t = 1 to 6
        TotalCount = Worksheets("Data").Cells(i+10,t+1).Value + TotalCount
    Next t
    Worksheets("Data").Cells(i+10,11).Value = TotalCount
    TotalCount = 0 'reset TotalCount
Next i

As long as you don't mind the macro going in the opposite direction, that should achieve what you're looking for.

2 Comments

I tried this one and it suited my needs best -- thanks. I'm curious (and trying to learn). Why did you add a number after the looped letters (eg 'i + 10... and t+1). Why not just declare 'i = 11 to 14' and 't = 1 to 6'?
I guess mostly out of habit. You could absolutely set it the way you described, I just like starting from 1. Maybe in my mind it makes changing the output position easier, or if you want to increment something while you loop it's easier. Comes down to preference.
0

This could be replaced with formulas in the H cells.

Or VBA:

Dim i As Long
Dim rngA As Range: Set rngA = Worksheets("Data").Range("B14:G14")
Dim rngB As Range: Set rngB = Worksheets("Data").Range("H14")

For i = 0 To 3
    rngB.Offset(-i, 0).Value = Application.Sum(rngA.Offset(-i, 0))
Next

You can also:

With Sheets("Data")
    .Range("H14") = Application.Sum(.Range("B14:G14"))
    ....
End With

Comments

0

We can use a Loop:

Sub UseLoop()
   With Sheets("Data")
   For i = 11 To 14
      TotalCount = Application.Sum(.Range("B" & i & ":G" & i))
      .Range("H" & i) = TotalCount
   Next i
   End With
End Sub

Comments

0

You do not need a loop, just a bit of magic :)

Worksheets("Data").Range("H11:H14").Formula = "=B:B+C:C+D:D+E:E+F:F+G:G"

If you do not like the formula add

Range("H11:H14").Value2 = Range("H11:H14").Value2

4 Comments

Wouldn't that sum the entirety of columns B, C, D, E, F, & G and put the results in the 4 cells H11:H14?
It sums only rows 11:14 in columns B through G.
Interesting! I just tried this out in a new worksheet - I learn something new every day. I saw B:B as a full column reference - I didn't know that leaving the row out caused it to use the current row.
Yes, Excel implicitly considers the same subset of rows as the one in the left hand side expression. It is cool :)
0
Range("H11:H14").Formula = "=SUM(B11:G11)"

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.