Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 68
Default add sum with macro to list

Hi,

How can I add a SUM function via a macro underneath a list which can
differ in length.
I know how to get to the bottom of the list using CTRL-DOWN in
"relative mode" but then I get into trouble with the range in the
formula.

Any help on this is welcome.

Cheers,

Harold

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,101
Default add sum with macro to list

Try this will sum A2 to last row
With Worksheets("Sheet1")
If IsEmpty(.Cells(.Rows.Count, 1)) Then
With .Cells(.Rows.Count, 1).End(xlUp)
.Offset(2, 0).Formula = "=Sum($A$2:" & _
.Address & ")"
End With
End If
End With

"mohavv" wrote:

Hi,

How can I add a SUM function via a macro underneath a list which can
differ in length.
I know how to get to the bottom of the list using CTRL-DOWN in
"relative mode" but then I get into trouble with the range in the
formula.

Any help on this is welcome.

Cheers,

Harold


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default add sum with macro to list

This uses column A to find the next row:

Option Explicit
Sub testme()
Dim NextRow As Long
With ActiveSheet
'use column A to determine the NextRow
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(NextRow, "A").FormulaR1C1 = "=sum(r1c:r[-1]c)"
End With
End Sub

If you wanted to fill in (say) columns B:F with the similar formula (avoiding
column A):

Option Explicit
Sub testme()
Dim NextRow As Long
With ActiveSheet
'use column A to determine the NextRow
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(NextRow, "B").Resize(1, 5).FormulaR1C1 = "=sum(r1c:r[-1]c)"
End With
End Sub

A note about the .FormulaR1C1 reference style:
=sum(r1c:r[-1]c)
r1c is the first row in the same column as the cell with the formula (Row 1,
same column)

r[-1]c is one cell up from the cell with the formula in the same column.

If you wanted Row 2 through one cell up, you'd use:
..FormulaR1C1 = "=sum(r2c:r[-1]c)"



mohavv wrote:

Hi,

How can I add a SUM function via a macro underneath a list which can
differ in length.
I know how to get to the bottom of the list using CTRL-DOWN in
"relative mode" but then I get into trouble with the range in the
formula.

Any help on this is welcome.

Cheers,

Harold


--

Dave Peterson
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
Sort a list macro. John Excel Discussion (Misc queries) 1 April 28th 06 11:49 PM
Macro for list sweetsue516 Excel Discussion (Misc queries) 0 March 31st 06 08:52 PM
delete a macro that isn't in macro list Jane Makinson Excel Discussion (Misc queries) 3 March 13th 06 01:10 PM
List Box with macro VETcalc Excel Worksheet Functions 2 February 28th 06 04:05 PM
How to create a macro that compares a list to another list Rampa New Users to Excel 1 January 13th 05 01:15 PM


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

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"