Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Worksheet Function - Calculations 2

Status
Not open for further replies.

VincentCrimmins

Programmer
Jan 16, 2008
37
0
0
IE
Hi,

I have a list of values, both positive and negative ranging between -5 and +5, defined in VBA as:

DailyPerf = Range("B2:B" & LastRowDailyPerf)

I've calculated the Average as follows:

Cells(20, 7) = Excel.WorksheetFunction.Avg(DailyPerf)

I want to calculate the average of the positive values. Is it possible to do this using the format for calculating the Average in Cells(20,7)?

Thanks!
 



Code:
[b]
Set [/b]DailyPerf = Range("B2:B" & LastRowDailyPerf)
Cells(20, 7).value = Excel.WorksheetFunction.Avg([DailyPerf])

Skip,
[sub]
[glasses]Did you hear what happened when the OO programmer lost his library?...
He's now living in OBJECT poverty![tongue][/sub]
 
Maybe direct definition: SumIF(...)/CountIf(...)

combo
 
Something like:

Count=0
running=0

For each cell in range

if cell.value > 0 then
count = count + 1
running = running + cell.value
end if

PosAvg = running/count

I'll leave working out the actual syntax to you.
 
Hi Skip,

I want to calculate the average of the positive values only.

If I was to do this in Excel I'd put it in a cell as:

{=AVERAGE(IF('Daily Perf'!B3:B1925>0,'Daily Perf'!B3:B1925))}

But I want to code this in VBA, similar to:

Cells(20, 7) = Excel.WorksheetFunction.Avg(DailyPerf)

Any ideas?

Thanks for your help...
 
Hi Combo,

I tried: Cells(7, 7) = Excel.WorksheetFunction.SumIf(DailyPerf, ">0", DailyPerf)

but it's telling me that an object is required, can you see what's wrong with my syntax?

Thanks...
 
Have you set DailyPref range for vba (Skip's first post)?BTW, there is no need for third argument in SumIf.

combo
 



Check out the SUBTOTAL function with the AVERAGE operator. Use the AutoFilter to display only positive values

Skip,
[sub]
[glasses]Did you hear what happened when the OO programmer lost his library?...
He's now living in OBJECT poverty![tongue][/sub]
 
Hi VincentCrimmins:

You may also want to try ...
Code:
Set refRange = Range("B2:B" & LastRowDailyPerf)
    ActiveWorkbook.Names.Add Name:="yRefRange", RefersTo:=refRange
    Cells(20, 7).FormulaArray = "=Average(if(yrefRange>0,yrefRange))"

Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top