Sum column totals in marcro

tanyaleblanc

Board Regular
Joined
Mar 16, 2019
Messages
145
I'm looking to sum the column totals in column O to T and have the totals appears in Cell O1 to T1
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Just put the formula:
Code:
=SUM(O2:O1048576)
in cell O1 and copy to the other columns (assuming you are using newer version of Excel; if not change last row number).

If you really want it in VBA, just turn on the Macro Recorder, and perform those same steps manually, and you will have code that will do what you want.
 
Upvote 0
I don't know what the last row is, this month is could be O3 to T3 and last row (last row could be row 6788 this month and next month it could be 7156).

I also want the totals to populate in cell O1 to T1
 
Upvote 0
I don't know what the last row is, this month is could be O3 to T3 and last row (last row could be row 6788 this month and next month it could be 7156).
If you use the formula I posted, which goes down to the last possible of Excel, it won't be an issue. Any blank cells to your sum is like adding zero. It won't change the value. And it is impossible to have anything past the last possible row of Excel, so you won't miss anything. Also, the advantage to this method is if you add more data, you don't ever need to adjust your formula (where if you have it just go down to the exact last row, you would need to re-run the macro code.

I also want the totals to populate in cell O1 to T1
As I mentioned, simply copy the the formula from O1 to cells P1:T1.

However, if you really want a macro that finds the last row in each column, and only does the sum down that far, try this:
Code:
Sub MyTotals()

    Dim cell As Range
    Dim col As Long
    Dim lr As Long
        
    For Each cell In Range("O1:T1")
'       Get column number of cell
        col = cell.Column
'       Find last row with data in that column
        lr = Cells(Rows.Count, col).End(xlUp).Row
'       Populate formula
        cell.FormulaR1C1 = "=SUM(R[1]C:R[" & lr - 1 & "]C)"
    Next cell
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,335
Messages
6,130,096
Members
449,557
Latest member
SarahGiles

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