#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Autosum

I have written a macro in excel, which needs to transfer a figure from one
part of the spreadsheet to another. However, the required figure is the total
of a variable number of data entries. So, before I can run the macro, I have
to click on the autosum button on the toolbar. I want to have this as part of
the macro, but whenever I try and record it in a macro, if there are, say 5
figures to be totalled and then transferred, the macro records the 5 cells.
Howvere, that is no good to me if the next figure is the total of, say, 7 or
2 cells - the macro calculates from 5 cells. How can I get the macro to work
like it does when you hit the autosum button - it recognises how many cells
are in the column to be added together, whether it be 2, 5, 7 etc?


--
AW
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Autosum

Maybe you can just do the equivalent in code.

This sample looks at the cell above and then does End|UpArrow to find out what
should be summed.

Option Explicit
Sub testme()
Dim RngToAutoSum As Range
Dim myCell As Range
Dim TopCell As Range

Set myCell = ActiveCell

With myCell
If IsEmpty(.Offset(-1, 0).Value) Then
Beep 'nothing right above
Else
If IsEmpty(.Offset(-2, 0).Value) Then
Set TopCell = .Offset(-1, 0)
Else
Set TopCell = .Offset(-1, 0).End(xlUp)
End If

Set RngToAutoSum = .Parent.Range(.Offset(-1, 0), TopCell)
myCell.Formula = "=sum(" & RngToAutoSum.Address(0, 0) & ")"
End If
End With

End Sub


If you're not using =sum(), you can modify the code. If you're not using the
cells above, you can modify the code to use rows.

And you can add as many checks as you want.


Alan wrote:

I have written a macro in excel, which needs to transfer a figure from one
part of the spreadsheet to another. However, the required figure is the total
of a variable number of data entries. So, before I can run the macro, I have
to click on the autosum button on the toolbar. I want to have this as part of
the macro, but whenever I try and record it in a macro, if there are, say 5
figures to be totalled and then transferred, the macro records the 5 cells.
Howvere, that is no good to me if the next figure is the total of, say, 7 or
2 cells - the macro calculates from 5 cells. How can I get the macro to work
like it does when you hit the autosum button - it recognises how many cells
are in the column to be added together, whether it be 2, 5, 7 etc?

--
AW


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Autosum

Thank you very much for your help - I will try this out and see how I get on
with it...
--
AW


"Dave Peterson" wrote:

Maybe you can just do the equivalent in code.

This sample looks at the cell above and then does End|UpArrow to find out what
should be summed.

Option Explicit
Sub testme()
Dim RngToAutoSum As Range
Dim myCell As Range
Dim TopCell As Range

Set myCell = ActiveCell

With myCell
If IsEmpty(.Offset(-1, 0).Value) Then
Beep 'nothing right above
Else
If IsEmpty(.Offset(-2, 0).Value) Then
Set TopCell = .Offset(-1, 0)
Else
Set TopCell = .Offset(-1, 0).End(xlUp)
End If

Set RngToAutoSum = .Parent.Range(.Offset(-1, 0), TopCell)
myCell.Formula = "=sum(" & RngToAutoSum.Address(0, 0) & ")"
End If
End With

End Sub


If you're not using =sum(), you can modify the code. If you're not using the
cells above, you can modify the code to use rows.

And you can add as many checks as you want.


Alan wrote:

I have written a macro in excel, which needs to transfer a figure from one
part of the spreadsheet to another. However, the required figure is the total
of a variable number of data entries. So, before I can run the macro, I have
to click on the autosum button on the toolbar. I want to have this as part of
the macro, but whenever I try and record it in a macro, if there are, say 5
figures to be totalled and then transferred, the macro records the 5 cells.
Howvere, that is no good to me if the next figure is the total of, say, 7 or
2 cells - the macro calculates from 5 cells. How can I get the macro to work
like it does when you hit the autosum button - it recognises how many cells
are in the column to be added together, whether it be 2, 5, 7 etc?

--
AW


--

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
how do i set up autosum as a % Robert s Excel Worksheet Functions 1 February 7th 07 08:18 PM
autosum Kevin Excel Worksheet Functions 6 November 25th 06 06:51 PM
AutoSum Carmel R Excel Worksheet Functions 4 November 6th 06 08:34 PM
Autosum box too big [email protected] Excel Worksheet Functions 1 February 9th 05 01:31 AM
autosum ?? golfer18 Excel Worksheet Functions 1 January 28th 05 10:29 PM


All times are GMT +1. The time now is 04:26 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"