Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default autosum in VBA

If I am writing an Excel VBA macro with relative references turned on, how do
i write code that creates a Sum formula that adds up a different number of
rows each time?

When I record the macro it seems to specify the exact number of rows it is
using in the formula. But I need the code to create the formula based on the
number of cells selected, not the same number of cells every time.

How do I do this?

Thanks if you have an answer!!

Mark
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default autosum in VBA

Mark,

Try something along the lines of:

Sub MyAutoSum()
With ActiveCell
.Formula = "=SUM(" & _
Range(.offset(-1, 0), _
.End(xlUp).End(xlUp)).Address(False, False) & ")"
End With
End Sub

Select a cell at the bottom of an arbitrarily long list of numbers, and run
it.

HTH,
Bernie
MS Excel MVP


"Mark" wrote in message
...
If I am writing an Excel VBA macro with relative references turned on, how
do
i write code that creates a Sum formula that adds up a different number of
rows each time?

When I record the macro it seems to specify the exact number of rows it is
using in the formula. But I need the code to create the formula based on
the
number of cells selected, not the same number of cells every time.

How do I do this?

Thanks if you have an answer!!

Mark



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
autosum capital me Excel Discussion (Misc queries) 3 April 5th 09 05:28 AM
Autosum Alan[_3_] Excel Discussion (Misc queries) 2 March 19th 07 12:35 PM
AutoSum Alectrical Excel Worksheet Functions 4 August 11th 06 10:43 PM
Autosum Stuart Perry Excel Discussion (Misc queries) 5 July 19th 05 08:45 PM


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