#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Sum Columns

Another post got me interested in this. It's desired to put the sum at
the bottom of columns C through E. I want to put the sum there, not a
formula. I thought some kind of R1C1 notation would be necessary, but
I got this method from help. The notation seems odd but it works well.
Is this the best way to do it?
TIA, James

Sub SumCols()
Dim k As Integer, LastRow As Long, myRg As Range
For k = 3 To 5
LastRow = Cells(65536, k).End(xlUp).Row
Set myRg = Range(Cells(2, k), Cells(LastRow, k))
Cells(LastRow + 1, k) = Application.WorksheetFunction.Sum(myRg)
Next k
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Sum Columns

Nothing wring with that per se.

You shouldn't hard-code the number of rows though, and you could also use
Resize

Sub SumCols()
Dim k As Integer, LastRow As Long, myRg As Range
For k = 3 To 5
LastRow = Cells(Rows.Count, k).End(xlUp).Row
Set myRg = Cells(2, k).Resize(LastRow -1)
Cells(LastRow + 1, k) = Application.WorksheetFunction.Sum(myRg)
Next k
End Sub

--
HTH

Bob Phillips

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

"Zone" wrote in message
oups.com...
Another post got me interested in this. It's desired to put the sum at
the bottom of columns C through E. I want to put the sum there, not a
formula. I thought some kind of R1C1 notation would be necessary, but
I got this method from help. The notation seems odd but it works well.
Is this the best way to do it?
TIA, James

Sub SumCols()
Dim k As Integer, LastRow As Long, myRg As Range
For k = 3 To 5
LastRow = Cells(65536, k).End(xlUp).Row
Set myRg = Range(Cells(2, k), Cells(LastRow, k))
Cells(LastRow + 1, k) = Application.WorksheetFunction.Sum(myRg)
Next k
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Sum Columns

Thanks, Bob. Interesting. Will study. James
Bob Phillips wrote:
Nothing wring with that per se.

You shouldn't hard-code the number of rows though, and you could also use
Resize

Sub SumCols()
Dim k As Integer, LastRow As Long, myRg As Range
For k = 3 To 5
LastRow = Cells(Rows.Count, k).End(xlUp).Row
Set myRg = Cells(2, k).Resize(LastRow -1)
Cells(LastRow + 1, k) = Application.WorksheetFunction.Sum(myRg)
Next k
End Sub

--
HTH

Bob Phillips

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

"Zone" wrote in message
oups.com...
Another post got me interested in this. It's desired to put the sum at
the bottom of columns C through E. I want to put the sum there, not a
formula. I thought some kind of R1C1 notation would be necessary, but
I got this method from help. The notation seems odd but it works well.
Is this the best way to do it?
TIA, James

Sub SumCols()
Dim k As Integer, LastRow As Long, myRg As Range
For k = 3 To 5
LastRow = Cells(65536, k).End(xlUp).Row
Set myRg = Range(Cells(2, k), Cells(LastRow, k))
Cells(LastRow + 1, k) = Application.WorksheetFunction.Sum(myRg)
Next k
End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make Columns A&B on Sheet2 always equal Sheet1 table columns A&B KnightBall Excel Worksheet Functions 6 August 18th 09 05:48 PM
putting 2 long columns into multiple columns in excel page and sor bob_mhc Excel Discussion (Misc queries) 1 April 25th 08 07:51 AM
to convert columns to rows having mulit independent group columns Quacy Excel Worksheet Functions 1 August 22nd 06 11:20 PM
Combine multiple columns into two long columns, Repeating rows in first column [email protected] Excel Discussion (Misc queries) 2 July 31st 06 09:45 PM
Combine multiple columns into two long columns, Repeating rows in first column [email protected] Excel Discussion (Misc queries) 0 July 31st 06 05:07 PM


All times are GMT +1. The time now is 02:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"