Summary in the last row

M

Mia

Hi,

I'm need to make a vba macro where the macro automaticly shall find
the last row and make a summary of the total column D. I cant get
It right, can someone please help?

So far I only has typed following.

Dim lngLastRow As Long
lngLastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
Range("D" & lngLastRow + 1) = _
 
D

Don Guillett

Sub slr()
dim mc as long
Dim lr As Long
mc = 4 'col D
lr = Cells(Rows.Count, mc).End(xlUp).Row
Cells(lr + 1, mc) = _
Application.Sum(Range(Cells(1, mc), Cells(lr, mc)))
End Sub
 
R

Ryan H

You can use this code. I assume Col. D has a header row so the code will
insert the SUM function in the cell below the last row with data in Col. D.
Plus, you don't have to reference the ActiveSheet, in VBA it is assumed you
mean the ActiveSheet. Hope this helps! If so, let me know, click "YES"
below.

Dim lngLastRow As Long

' find last row with data in Col.D
lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row

' insert sum formula
Cells(lngLastRow + 1, "D").Formula = "=SUM(D2:D" & lngLastRow & ")"
 
M

Mia

Hi again,

Thank you again for your help before.

If I want the answer (the sum) to be in a special format, what shall
I write? I want it to be bold and in curency (swedish kronor).
Do you know how to code this?


--
Best regards
Mia


"Don Guillett" skrev:
 
R

Ryan H

You would have to change properties on the Font collection.

Dim lngLastRow As Long

' find last row with data in Col.D
lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row

' insert sum formula
With Cells(lngLastRow + 1, "D")
.Formula = "=SUM(D2:D" & lngLastRow & ")"
.Font.Bold = True
.Font.Name = "Arial"
.NumberFormat = "$#,##0.00"
End With

Hope this helps! If so, let me know, click "YES" below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top