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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
As bsquad mentioned, you are missing your END IF statement, i.e.
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)))
[COLOR=#ff0000]    End If[/COLOR]
Next i
The only time you don't use an End If is if you put the THEN clause on the same line as your IF, or as a continuation, i.e.
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)))
Next i
or
Code:
For i = 4 To 12
    lr = Cells(Rows.Count, i).End(xlUp).Row
    If lr > 4 Then [B]_[/B]
        Cells(lr + 1, i) = WorksheetFunction.Sum(Range(Cells(5, i), Cells(lr, i)))
Next i
(note the location of the underscore after the word "Then"; it should be a single space followed by the underscore)
 
Last edited:
Upvote 0
Then there is no error with that part of the code. If you are getting an error, I am guessing it has to do with the rest of your code.
Can you post the entire code from the procedure you have this code snippet located in?
 
Upvote 0
Not sure what is going on. I used the code with the underscore and it worked fine.
Yes, that would, for the reasons I mentioned. What the underscore says is that what follows on the next line is a continuation of the current line.
So even though it is on different rows, it is treated as if it is all on the same row. People often use that for readability when they have really long lines of code.

Which lines exactly do you want to bold and highlight?
Just the cells that the Sums are going in, or the whole range?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,408
Members
449,448
Latest member
Andrew Slatter

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