View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_3_] Jim Thomlinson[_3_] is offline
external usenet poster
 
Posts: 983
Default Beginner looping / summarize question

There are lots of ways to skin a cat. Here is what I might call the beginner
way as it is probably easiest to understand.

function AddStuff() as double
dim dblReturnValue as double

sheets("Sheet1").range("A2").select
do while activecell.value < ""
dblReturnvalue = dblreturnvalue + activecell.offset(0,1).value
activecell.offset(1,0).select
loop

addstuff = dblreturnvalue
end function

Here it is a little more advanced using range objects. I am not moving the
active cell which can be very advantageous...

function AddStuff() as double
dim dblReturnValue as double
dim rngCell as range
dim rngAllPeriods

set rngAllPeriods = range(sheet1.range("A1"), sheet1.Range("A1").end
xlDown)).offset(0,1)

for each rngcell in rngallperiods
dblreturnvalue = dblreturnvalue = rngCell.value
next rngcell

addstuff = dblreturnvalue
end function

And without a loop...

function AddStuff() as double

addstuff = application.sum(range(sheet1.range("A1"), sheet1.Range("A1").end
xlDown)).offset(0,1))

end function

HTH

"PGJ" wrote:

How do you set up code that loops through a range summarizing items that
meet a certain criteria?

For example, If the date in column A = 01-31-05 then add up the amounts.

Column A Colume B
01-31-05 50.00
01-31-05 100.00
02-01-05 200.00

desired result would be 150.00.

What would the code look like?

Thanks in advance