Solved

Total each row for unknown Rows N

Posted on 2013-10-30
4
253 Views
Last Modified: 2013-10-31
How do I total each row (x), for all seven days?  The number of rows will continue to increase each week (variable Row N). I'd like to do this in VBA.  I need to be able to find the last row (Row N) automatically and sum (Total), for each row.

Row#      Sat      Sun       Mon      Tues      Wed      Thurs      Fri      Total
1                  5      5      5      5      2.5      x
2                  0      0      0      0      1.25      x
3                  2      3      3      3      4.25      x
4                  8      8      8      8      0      x
5                  0      0      0      0      8      x
6                  6      6      5      5      5      x
7                  3      2      4      3      3      x
8                  4      12      2.5      2      2      x
9                  2      3      1.5      0.5      0      x
10                  1      0.5      0      0      2      x
11                  5      5      5      5      2.5      x
.                  0      0      0      0      1.25      x
.                  2      3      3      3      4.25      x
.                  8      8      8      8      0      x
.                  0      0      0      0      8      x
.                  6      6      5      5      5      x
.                  3      2      4      3      3      x
.                  4      4      2.5      2      2      x
.                  2      3      1.5      0.5      0      x
Row N                  1      0.5      0      0      2      x
0
Comment
Question by:dm2121
  • 2
  • 2
4 Comments
 
LVL 80

Expert Comment

by:byundt
ID: 39612788
Here is a macro that will put the sum formulas on the active worksheet. As written, it is expecting to see the data to be summed in columns B:H, starting in row 2.
Sub Totalizer()
Dim rgData As Range, rgTotals As Range
With ActiveSheet
    Set rgData = Intersect(.UsedRange, .Range("B:H"))       'Data in columns B:H
    Set rgData = rgData.Offset(1, 0).Resize(rgData.Rows.Count - 1)     'Start in row 2
    Set rgTotals = rgData.Columns(1).Offset(0, 7)
End With
rgTotals.FormulaR1C1 = "=SUM(RC" & rgData.Column & ":RC" & (rgData.Column + rgData.Columns.Count - 1) & ")"
End Sub

Open in new window

0
 

Author Comment

by:dm2121
ID: 39613936
Thanks byundt for the quick response!

It seems to work, but I'd like it to find the last row and sum the days from row 2 to Row N; not Row N+1. This week I could have 500 rows and need to total the days; next week it will grow to1149 rows; I don't need to go down 10,000.  I don't want to have "0" in the total after Row N+1.

dm2121
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39613963
The code is finding the bottom of your data using the .UsedRange property of the worksheet. There is apparently something in row N+1.

I can alternatively find the last row by looking upwards from the bottom of the worksheet in say column B.
Sub Totalizer()
Dim rgData As Range, rgTotals As Range
Dim N As Long
With ActiveSheet
    N = .Cells(.Rows.Count, "B").End(xlUp).Row          'Find last row of data by looking up from bottom of column B
    Set rgData = Range(.Range("B2"), .Range("H" & N))   'All data in columns B:H, starting in row 2
    Set rgTotals = rgData.Columns(1).Offset(0, 7)
End With
rgTotals.FormulaR1C1 = "=SUM(RC" & rgData.Column & ":RC" & (rgData.Column + rgData.Columns.Count - 1) & ")"
End Sub

Open in new window

0
 

Author Comment

by:dm2121
ID: 39614032
byunt,

You are correct! Your solution is perfect and works as planned!  

Please disregard my first comment; the problem came from my end.

Thank you very much for your great expertize!

Regards,

dm2121
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now