sum column vba

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
This is an additional question from a previous post.

I received the following code, however when using in a different worksheet it is not working well. I am trying to place a total row (sum) and format this row. Possibly bold font & fill


The range is D5:Lxx). Column 12 may expand.

Dim lr As Long

For i = 4 To 12
lr = Cells(Rows.Count, i).End(xlUp).Row
If lr > 4 Then
Cells(lr + 1, i) = WorksheetFunction.Sum(Range(Cells(5, i), Cells(lr, i)))

Next i
 
Then do it like this (note that we know need to get rid of our underscore and use an END IF since we have multiple commands being returned in our IF ... THEN block):
Code:
For i = 4 To 12
    lr = Cells(Rows.Count, i).End(xlUp).Row
    If lr > 4 Then _
        Cells(lr + 1, i) = WorksheetFunction.Sum(Range(Cells(5, i), Cells(lr, i)))
        Cells(lr + 1, i).Font.Bold = True
        Cells(lr + 1, i).Interior.Color = 65535
Next i
If you would like to use some color other than yellow, just turn on your Macro Recorder, record yourself filling a cell with the color you want, stop the Macro Recorder and view your recorded code to see what that color code is, and then make the change to the code above.
 
Last edited:
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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