ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   How to use macro to set "Sum" function (https://www.excelbanter.com/excel-worksheet-functions/209415-how-use-macro-set-sum-function.html)

New user

How to use macro to set "Sum" function
 
Do anyone know if i want to set a macro which i want to comprise 2 elements:

1: total underline
2: sum ("Sum" function in Excel which automatically sum up all the cells
with figures)

Every time i set, the no. of cells to be added are fixed. for example, Col A
has 3 cells to sum up; when i apply it to Col B which has 5 cells, it also
only add up the last 3 cells, not 5.

Can anyone advise me on this matter?

Thank you so much.


--
New user

Roger Govier[_3_]

How to use macro to set "Sum" function
 
Hi

Try the following macro.
It assumes you have selected the range of cells you wish to Sum, then run
the macro.

Sub addup()

Dim i As Long, lr As Long, j As Long
Dim rng As String, rng1 As String, rng2 As String
With Selection
i = .Row
j = .Column
lr = .Rows.Count + 1
End With
rng1 = Cells(i, j).Address
rng2 = Cells(lr - 1, j).Address
Cells(lr, j) = "=SUM(" & rng1 & ":" & rng2 & ")"
With Cells(lr, j).Borders(xlEdgeTop)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With

End Sub

--
Regards
Roger Govier

"New user" wrote in message
...
Do anyone know if i want to set a macro which i want to comprise 2
elements:

1: total underline
2: sum ("Sum" function in Excel which automatically sum up all the cells
with figures)

Every time i set, the no. of cells to be added are fixed. for example, Col
A
has 3 cells to sum up; when i apply it to Col B which has 5 cells, it also
only add up the last 3 cells, not 5.

Can anyone advise me on this matter?

Thank you so much.


--
New user




All times are GMT +1. The time now is 07:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com