Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Sum a range of cells based on relative position

I have a VBA construct like this one

dim SCell as Range
for each SCell in Selection.Cells
{snip}
next SCell
.... where the selected cells are 1 column wide and several rows deep.

As the For... Next spins through the cells in the selected range, I
need to take some actions if the sum of a range of cells on a given
row is greater than zero. So, if SCell is at cell B15, I need to find
out the sum of cells D15:G15.

What is the code to derive that sum? I'm thinking it's some use of
OFFSET, perhaps, but I'm not sure of the syntax. Any ideas?

Thanks,
Dave O
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Sum a range of cells based on relative position


Set sCell = Range("B15")
x = Application.WorksheetFunction.Sum(sCell.Offset(0, 2).Resize(1, 4))
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
("List Files" finds files/folders and creates hyperlinked lists)




"DaveO"
wrote in message
...
I have a VBA construct like this one

dim SCell as Range
for each SCell in Selection.Cells
{snip}
next SCell
... where the selected cells are 1 column wide and several rows deep.

As the For... Next spins through the cells in the selected range, I
need to take some actions if the sum of a range of cells on a given
row is greater than zero. So, if SCell is at cell B15, I need to find
out the sum of cells D15:G15.

What is the code to derive that sum? I'm thinking it's some use of
OFFSET, perhaps, but I'm not sure of the syntax. Any ideas?

Thanks,
Dave O



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default Sum a range of cells based on relative position

Thanks, Jim, I'll give it a shot. Because the relative position
changes with each loop, I'll need to modify your code to read

Set sCell = Range(sCell.Address)

.... but that's exactly what I was looking for. Thanks!
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Sum a range of cells based on relative position

My example was misleading in that the Set code line was only there is indicate the reference cell.
If you are looping thru each cell in the selection, then sCell is automatically "Set" by Excel.
You don't need to and shouldn't set a reference to it during the loop.
On the other hand, the Offset and Resize parameters can be changed to reference different cell
locations.

Also, it is good practice to specify the parent sheet for a range.
Instead of "Range("B17") use Worksheets("Sludge").Range("B17")
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(Extras for Excel is useful)



"DaveO"
wrote in message
...
Thanks, Jim, I'll give it a shot. Because the relative position
changes with each loop, I'll need to modify your code to read

Set sCell = Range(sCell.Address)

... but that's exactly what I was looking for. 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
formula based on continuous relative cells bwilde Excel Discussion (Misc queries) 0 February 23rd 10 03:52 PM
Excel Formula using relative position of cells in two different worksheets David Virgil Hobbs Excel Discussion (Misc queries) 1 December 14th 06 03:36 AM
Excel Formula using relative position of cells in two different worksheets David Virgil Hobbs Excel Worksheet Functions 1 December 14th 06 03:36 AM
Excel Formula using relative position of cells in two different worksheets David Virgil Hobbs Excel Programming 1 December 14th 06 03:36 AM
Lookup with search range start based on position of last blank lin rcmodelr Excel Worksheet Functions 0 November 14th 04 06:32 AM


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