sum of a range starting from cell in fixed column to cell in last column

SamP1983

New Member
Joined
Jul 29, 2015
Messages
7
Hello,

I am building a dashboard which contains a revenue overview (monthly). The user of the dashboard selects the start month and end month (e.g. January - July). It would then look like this

SERVICESJanuaryFebruaryMarchAprilMayJuneJulyTotal
Product1152345435156549141234581645127512487157481sum
Product232457387546451754781512045417848761sum


I am trying to find a VBA code to calculate the sum per product. This range per product always starts with a fixed cell and has to end with the cell of the last column within the same row. However the last column can change (could be any month of the year).

I already found a way to capture the last column number using this:

PHP:
Dim LastCol As Long
    
    With ActiveSheet
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With
    
    Cells(2, LastCol).Value = WorksheetFunction.Sum(Cells(2,2) ?????)

But I got stuck creating a WorksheetFunction.Sum that will calculate the sum per product for all months in each row (with a variable end month).

Does any one would have any advise?

Thank you in advance!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This could easily be achieved via formulae, but if your forced to use a VBA approach, you're nearly there.

Your last line of code should be:
Code:
Cells(2,LastCol).Value = Worksheetfunction.Sum(Cells(2,2),Cells(2,LastCol-1))
 
Upvote 0
Hi Neil,

Thank you for your reply! I noticed that when I use the last line as you suggested it will only calculate the sum of the first and the last month (January + July in this case). However I am looking for a code that will calculate all the months (January + February + March + April + May + June + July in this example).

Would you have any other suggestions?

Thanks in advance!
 
Upvote 0
Hi Sam,

You need to use a Range in your case.

Code:
Cells(2,LastCol).Value = Worksheetfunction.Sum(Range(Cells(2,2),Cells(2,LastCol-1)))
 
Upvote 0

Forum statistics

Threads
1,215,970
Messages
6,127,991
Members
449,414
Latest member
sameri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top