Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using a Macro to Sum a Variable-Length Range (a Column)


I created a string formula in the macro to calculate the sum. This
formula includes a reference to a cell, not in the Range, that contains
the row number of the cell that is adjacent above the cell (the Active
Cell) that is to contain the sum formula.

This is the line of code: ActiveCell.FormulaR1C1 = " = ""=sum(J3:J"" &
R[4]C & "")"" " (SPACES ADDED FOR CLARITY). The cell containing the Row
number is 4 rows belows the cell to contain the sum formula.

The macro then does a copy, pastespecial, values on the Active Cell and
the above formula is calculated to be =sum(J3:J23) (ASSUMING THAT 23 IS
THE ROW NUMBER IMMEDIATELY ABOVE THE ACTIVE CELL). Having the macro do
another copy, pastespecial, values on this cell has zero effect.

I tried using Application.SendKeys "{F2}~", but it only wants to open
the Object Browser. If I click on the WorkSheet, key {F2}, then key
{ENTER}, the formula is calculated and the cell displays the numerical
result.

What am I doing wrong?
Chuckles123


--
Chuckles123
------------------------------------------------------------------------
Chuckles123's Profile: http://www.excelforum.com/member.php...o&userid=14948
View this thread: http://www.excelforum.com/showthread...hreadid=265814

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Using a Macro to Sum a Variable-Length Range (a Column)

You have .formular1c1 in your code--but you put J3:J in the formula.

You're gonna have to use R1C1 style or A1 style--but not a mixture.

I'm confused about what row you're returning, but maybe this'll give you an
idea:

With ActiveCell
.Formula = "=sum(J3:J" & .Offset(4, 0).Value & ")"
.value = .value 'convert to values
End With



Chuckles123 wrote:

I created a string formula in the macro to calculate the sum. This
formula includes a reference to a cell, not in the Range, that contains
the row number of the cell that is adjacent above the cell (the Active
Cell) that is to contain the sum formula.

This is the line of code: ActiveCell.FormulaR1C1 = " = ""=sum(J3:J"" &
R[4]C & "")"" " (SPACES ADDED FOR CLARITY). The cell containing the Row
number is 4 rows belows the cell to contain the sum formula.

The macro then does a copy, pastespecial, values on the Active Cell and
the above formula is calculated to be =sum(J3:J23) (ASSUMING THAT 23 IS
THE ROW NUMBER IMMEDIATELY ABOVE THE ACTIVE CELL). Having the macro do
another copy, pastespecial, values on this cell has zero effect.

I tried using Application.SendKeys "{F2}~", but it only wants to open
the Object Browser. If I click on the WorkSheet, key {F2}, then key
{ENTER}, the formula is calculated and the cell displays the numerical
result.

What am I doing wrong?
Chuckles123

--
Chuckles123
------------------------------------------------------------------------
Chuckles123's Profile: http://www.excelforum.com/member.php...o&userid=14948
View this thread: http://www.excelforum.com/showthread...hreadid=265814


--

Dave Peterson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Using a Macro to Sum a Variable-Length Range (a Column)

So, in your example the active cell is J24 and you need the formula in J24
to be =SUM(J2:J23)
also, the '23' is variable, but wikk be the row above the active cell.

DIM Target as Range ' we'll use this to point at the formula cell
SET Target = Range("J3").End(XLDown).offset(1,0)
Target.FormulaR1C1 = "=SUM(R3C:R[-1]C"


if you need to convert the formula to a value, just add
Target.Value = Target.Value



--
Patrick Molloy
Microsoft Excel MVP
---------------------------------
I Feel Great!
---------------------------------
"Chuckles123" wrote in message
...

I created a string formula in the macro to calculate the sum. This
formula includes a reference to a cell, not in the Range, that contains
the row number of the cell that is adjacent above the cell (the Active
Cell) that is to contain the sum formula.

This is the line of code: ActiveCell.FormulaR1C1 = " = ""=sum(J3:J"" &
R[4]C & "")"" " (SPACES ADDED FOR CLARITY). The cell containing the Row
number is 4 rows belows the cell to contain the sum formula.

The macro then does a copy, pastespecial, values on the Active Cell and
the above formula is calculated to be =sum(J3:J23) (ASSUMING THAT 23 IS
THE ROW NUMBER IMMEDIATELY ABOVE THE ACTIVE CELL). Having the macro do
another copy, pastespecial, values on this cell has zero effect.

I tried using Application.SendKeys "{F2}~", but it only wants to open
the Object Browser. If I click on the WorkSheet, key {F2}, then key
{ENTER}, the formula is calculated and the cell displays the numerical
result.

What am I doing wrong?
Chuckles123


--
Chuckles123
------------------------------------------------------------------------
Chuckles123's Profile:
http://www.excelforum.com/member.php...o&userid=14948
View this thread: http://www.excelforum.com/showthread...hreadid=265814



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
Variable series length/range JessK Charts and Charting in Excel 1 March 3rd 06 04:02 AM
Sum a Column of Variable length Chris G Excel Discussion (Misc queries) 4 November 7th 05 12:25 PM
Performing a function on a column of variable length BeenThereGotLost Excel Worksheet Functions 3 July 1st 05 02:50 PM
Sum a column of variable length? Brian Excel Discussion (Misc queries) 5 February 3rd 05 02:26 PM
Averaging a variable length column Doug[_10_] Excel Programming 1 June 22nd 04 07:48 PM


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