Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Autosum variable range in macro

Hi Eric,

Here is one example of Visual Basic for Applications code to create a SUM
formula, based on the active cell, which sums contiguous data above it:

Sub testsum()
myrange = Range(ActiveCell.Offset(-1, 0).End(xlUp), ActiveCell.Offset(-1,
0)).Address(0, 0)
ActiveCell.Formula = "=sum(" & myrange & ")"
End Sub

Note that myrange is generated dynamically each time the macro is run. The
macro determines the range of contiguous data immediately above the active
cell, and creates the SUM formula based on that range. This method assumes
that all cells in the range to be summed contain data (no empty cells).

Hope this helps!
--
Tom Moore [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.


"Eric Dreshfield" wrote in message
...
I would like to be able to add programming to a macro that
mimics the AutoSum key....for example, if cells A1 through
A10 have values and the active cell is A11, I want the
macro to create the =Sum(a1:a10) formula, but I do not
want the cell references hard coded, as the number of rows
will vary each time this process is run. Any suggestions ?

Thanks !



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Autosum variable range in macro

Tom,

Thanks ! This worked just as I was hoping it would.

-----Original Message-----
Hi Eric,

Here is one example of Visual Basic for Applications code

to create a SUM
formula, based on the active cell, which sums contiguous

data above it:

Sub testsum()
myrange = Range(ActiveCell.Offset(-1, 0).End(xlUp),

ActiveCell.Offset(-1,
0)).Address(0, 0)
ActiveCell.Formula = "=sum(" & myrange & ")"
End Sub

Note that myrange is generated dynamically each time the

macro is run. The
macro determines the range of contiguous data immediately

above the active
cell, and creates the SUM formula based on that range.

This method assumes
that all cells in the range to be summed contain data (no

empty cells).

Hope this helps!
--
Tom Moore [MSFT]

This posting is provided "AS IS" with no warranties, and

confers no rights.


"Eric Dreshfield" wrote in message
...
I would like to be able to add programming to a macro

that
mimics the AutoSum key....for example, if cells A1

through
A10 have values and the active cell is A11, I want the
macro to create the =Sum(a1:a10) formula, but I do not
want the cell references hard coded, as the number of

rows
will vary each time this process is run. Any

suggestions ?

Thanks !



.

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
Macro to Sort A-Z a variable range of cells Keith B Excel Discussion (Misc queries) 1 September 9th 09 03:31 AM
Macro - Using a variable number of columns in a Range stumped Excel Discussion (Misc queries) 2 October 3rd 06 08:13 PM
Range used in a macro needs to be variable DaveP Excel Worksheet Functions 3 November 4th 05 03:23 PM
variable range in a macro AMK Excel Discussion (Misc queries) 3 July 6th 05 09:32 AM
Is it possible to set a variable range in a macro? Mary About this forum 0 June 12th 05 06:26 PM


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