ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How To Sum (https://www.excelbanter.com/excel-programming/373101-how-sum.html)

hon123456

How To Sum
 
Dear All,

I have an excel sheet which will be filled with data from
database dynamically, e.g.

Field_A Field_B

A 1
B 2
C 3
D 4

Now after the data has been filled, I want Excel to add a
Total sum following the last record.
just like following:

Field_A Field_B
A 1
B 2
C 3
D 4
Total 10

How can I do that in VBA?

Thanks.


Bob Phillips

How To Sum
 

LastRow = Cells(Rows.Count,"B").End(xlUp).Row
Cells(LastRow,"B").Formula = "=SUM(B1:B" & LastRow & ")"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"hon123456" wrote in message
ps.com...
Dear All,

I have an excel sheet which will be filled with data from
database dynamically, e.g.

Field_A Field_B

A 1
B 2
C 3
D 4

Now after the data has been filled, I want Excel to add a
Total sum following the last record.
just like following:

Field_A Field_B
A 1
B 2
C 3
D 4
Total 10

How can I do that in VBA?

Thanks.




Dave Peterson

How To Sum
 
I think that Bob will be writing over the last value in column B. Maybe...

Dim LastRow as Long
with activesheet
lastrow = .cells(.rows.count,"A").end(xlup).row
.cells(lastrow+1,"A").value = "Total"
.cells(lastrow+1,"B").formula = "=sum(b1:b" & lastrow & ")"
end with




Bob Phillips wrote:

LastRow = Cells(Rows.Count,"B").End(xlUp).Row
Cells(LastRow,"B").Formula = "=SUM(B1:B" & LastRow & ")"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"hon123456" wrote in message
ps.com...
Dear All,

I have an excel sheet which will be filled with data from
database dynamically, e.g.

Field_A Field_B

A 1
B 2
C 3
D 4

Now after the data has been filled, I want Excel to add a
Total sum following the last record.
just like following:

Field_A Field_B
A 1
B 2
C 3
D 4
Total 10

How can I do that in VBA?

Thanks.


--

Dave Peterson

hon123456

How To Sum
 
Thanks for you all,

My another question is how can I sum the result
horizontally?

Thanks


Bob Phillips

How To Sum
 
LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
Cells(2, LastCol + 1).FormulaR1C1 = "=SUM(R2C1:R2C" & LastCol & ")"


(and I remembered to offset the cell this time <g)

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"hon123456" wrote in message
ps.com...
Thanks for you all,

My another question is how can I sum the result
horizontally?

Thanks





All times are GMT +1. The time now is 11:45 PM.

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