Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default AutoSum macro that doesn't include headings

Excel Experts,

I wrote the below code which seems to be effective in putting an autosum
formula below a column of numbers such as this one. I select the cell below
the numbers and run the macro.

A B
1
2 100
3 200
4 50
5

However, how can I modify it so if I have a heading on my column of numbers,
or even two or three cells with text above my number column, the code will
start the range for the autosum with the top numerical cell of my column.

Thanks,
Alan


Sub AutoSum()

Dim SumCell As Range
Dim FirstCell As Range
Dim LastCell As Range

Set SumCell = Selection

Set FirstCell = SumCell.Offset(-1).End(xlUp)

Set LastCell = SumCell.Offset(-1)

With SumCell
.FormulaR1C1 = "=sum(" & FirstCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ":" & LastCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ")"
End With

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default AutoSum macro that doesn't include headings

Hi Achidsey,

One way of modifying your code might be:

'========================
Sub AutoSum2()

Dim SumCell As Range
Dim FirstCell As Range
Dim LastCell As Range
Dim Rng As Range
Dim rCell As Range
Dim RngFirstNumeric As Range

Set SumCell = Selection
Set FirstCell = SumCell.Offset(-1).End(xlUp)
Set LastCell = SumCell.Offset(-1)

Set Rng = Range(FirstCell, LastCell)

For Each rCell In Rng.Cells
If IsNumeric(rCell) Then
Set RngFirstNumeric = rCell
Exit For
End If
Next rCell

If Not RngFirstNumeric Is Nothing Then
With SumCell
.FormulaR1C1 = "=sum(" & RngFirstNumeric.Address _
(False, False, xlR1C1, RelativeTo:=.Item(1)) _
& ":" & LastCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ")"
End With
Else
'No numeric cell to sum!
End If

End Sub
'<<========================

As alternative approach, you could use the built in autosum control, as
demonstrated by Jim Rech in a NG post:

'========================
Sub DoAutoSum()
'// From: Jim Rech )Subject: AutoSum Toolbutton Execute
'// Newsgroups: microsoft.public.excel.programming Date: 2001-05-17 07:38:02
PST
'// In case anyone uses the Execute method with the AutoSum toolbar button I
'// just discovered it doesn'/// t work with Excel 2002 because the control
has
'// changed type. It'/// s now a msoControlSplitButtonPopup because it can
so
'// several math functions in addition to a Sum. So this is the way to
handle
'// it across versions I think:

Dim x As CommandBarControl
Set x = CommandBars.FindControl(ID:=226)
If Val(Application.Version) = 10 Then _
Set x = x.Controls(1)
x.Execute
If Selection.Cells.Count = 1 Then
x.Execute 'Again to exit edit mode if only one cell is selected
End If
End Sub
'<<========================


---
Regards,
Norman



"achidsey" (notmorespam) wrote in message
...
Excel Experts,

I wrote the below code which seems to be effective in putting an autosum
formula below a column of numbers such as this one. I select the cell
below
the numbers and run the macro.

A B
1
2 100
3 200
4 50
5

However, how can I modify it so if I have a heading on my column of
numbers,
or even two or three cells with text above my number column, the code will
start the range for the autosum with the top numerical cell of my column.

Thanks,
Alan


Sub AutoSum()

Dim SumCell As Range
Dim FirstCell As Range
Dim LastCell As Range

Set SumCell = Selection

Set FirstCell = SumCell.Offset(-1).End(xlUp)

Set LastCell = SumCell.Offset(-1)

With SumCell
.FormulaR1C1 = "=sum(" & FirstCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ":" & LastCell.Address(False, False,
xlR1C1, _
RelativeTo:=.Item(1)) & ")"
End With

End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default AutoSum macro that doesn't include headings

Norman,

Thanks very much for the assistance.

Alan


--
achidsey


"Norman Jones" wrote:

Hi Achidsey,

One way of modifying your code might be:

'========================
Sub AutoSum2()

Dim SumCell As Range
Dim FirstCell As Range
Dim LastCell As Range
Dim Rng As Range
Dim rCell As Range
Dim RngFirstNumeric As Range

Set SumCell = Selection
Set FirstCell = SumCell.Offset(-1).End(xlUp)
Set LastCell = SumCell.Offset(-1)

Set Rng = Range(FirstCell, LastCell)

For Each rCell In Rng.Cells
If IsNumeric(rCell) Then
Set RngFirstNumeric = rCell
Exit For
End If
Next rCell

If Not RngFirstNumeric Is Nothing Then
With SumCell
.FormulaR1C1 = "=sum(" & RngFirstNumeric.Address _
(False, False, xlR1C1, RelativeTo:=.Item(1)) _
& ":" & LastCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ")"
End With
Else
'No numeric cell to sum!
End If

End Sub
'<<========================

As alternative approach, you could use the built in autosum control, as
demonstrated by Jim Rech in a NG post:

'========================
Sub DoAutoSum()
'// From: Jim Rech )Subject: AutoSum Toolbutton Execute
'// Newsgroups: microsoft.public.excel.programming Date: 2001-05-17 07:38:02
PST
'// In case anyone uses the Execute method with the AutoSum toolbar button I
'// just discovered it doesn'/// t work with Excel 2002 because the control
has
'// changed type. It'/// s now a msoControlSplitButtonPopup because it can
so
'// several math functions in addition to a Sum. So this is the way to
handle
'// it across versions I think:

Dim x As CommandBarControl
Set x = CommandBars.FindControl(ID:=226)
If Val(Application.Version) = 10 Then _
Set x = x.Controls(1)
x.Execute
If Selection.Cells.Count = 1 Then
x.Execute 'Again to exit edit mode if only one cell is selected
End If
End Sub
'<<========================


---
Regards,
Norman



"achidsey" (notmorespam) wrote in message
...
Excel Experts,

I wrote the below code which seems to be effective in putting an autosum
formula below a column of numbers such as this one. I select the cell
below
the numbers and run the macro.

A B
1
2 100
3 200
4 50
5

However, how can I modify it so if I have a heading on my column of
numbers,
or even two or three cells with text above my number column, the code will
start the range for the autosum with the top numerical cell of my column.

Thanks,
Alan


Sub AutoSum()

Dim SumCell As Range
Dim FirstCell As Range
Dim LastCell As Range

Set SumCell = Selection

Set FirstCell = SumCell.Offset(-1).End(xlUp)

Set LastCell = SumCell.Offset(-1)

With SumCell
.FormulaR1C1 = "=sum(" & FirstCell.Address(False, False, xlR1C1, _
RelativeTo:=.Item(1)) & ":" & LastCell.Address(False, False,
xlR1C1, _
RelativeTo:=.Item(1)) & ")"
End With

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
How 2 sort an autosum total list after adding items 2 autosum item akm Excel Discussion (Misc queries) 0 May 30th 10 11:44 PM
Macro to AddRow and Autosum simplymidori[_2_] Excel Discussion (Misc queries) 4 April 29th 08 07:42 PM
create autosum macro along with notation PINO Excel Programming 0 June 15th 05 04:09 PM
Autosum variable range in macro Tim Zych[_2_] Excel Programming 0 September 4th 03 04:52 AM
Autosum variable range in macro Jeffrey Marcellus Excel Programming 1 September 3rd 03 10:36 PM


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

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

About Us

"It's about Microsoft Excel"