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

[table="width: 500, align: center"]
[tr]
[td]SERVICES[/td]
[td]January[/td]
[td]February[/td]
[td]March[/td]
[td]April[/td]
[td]May[/td]
[td]June[/td]
[td]July[/td]
[td]Total[/td]
[/tr]
[tr]
[td]Product1[/td]
[td]152345[/td]
[td]435156[/td]
[td]549141[/td]
[td]234581[/td]
[td]645127[/td]
[td]512487[/td]
[td]157481[/td]
[td]sum[/td]
[/tr]
[tr]
[td]Product2[/td]
[td]32457[/td]
[td]38754[/td]
[td]64517[/td]
[td]54781[/td]
[td]51204[/td]
[td]54178[/td]
[td]48761[/td]
[td]sum[/td]
[/tr]
[/table]


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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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