Excel help needed

Please post any questions regarding the program here.

Moderator: 2020vision

Excel help needed

Postby alrodopial » Thu Mar 25, 2010 10:05 am

Hi,
I want to find the minimum of a range with criteria using only VBA.

So the min of A1:A100 if all prices at this range are above 0 (>0 the criteria) with VBA
If it was a formula:
MIN(IF(A1:A100>0),A1:A100)) array formula

Any ideas?

I can loop through each cell in the range but it's a slow VBA this way
alrodopial
 
Posts: 1384
Joined: Wed Dec 06, 2006 9:59 pm

Postby osknows » Thu Mar 25, 2010 12:00 pm

Hello,

how about this function in a module?

Code: Select all
Function MinExZero1(ByVal rng As Range) As Double
Dim objFunc As WorksheetFunction

Set objFunc = Application.WorksheetFunction
With objFunc
MinExZero1 = .Min(rng)
If MinExZero1 = 0 Then
MinExZero1 = .Small(rng, .CountIf(rng, "=0") + 1)
End If
End With

End Function


then call it with something such as

Code: Select all
Sub test()

a = MinExZero1(Range("a1:a4"))

End Sub
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby Ian » Thu Mar 25, 2010 12:19 pm

Don't you just need to use Application.WorksheetFunction.Min on its own ? If it returns zero then all the values in the range weren't > 0.
Ian
 
Posts: 834
Joined: Sat Nov 19, 2005 8:35 am
Location: Birmingham

Postby osknows » Thu Mar 25, 2010 12:27 pm

Ian wrote:Don't you just need to use Application.WorksheetFunction.Min on its own ? If it returns zero then all the values in the range weren't > 0.


I thought the question was what is the minimum value excluding 0..?

Essentially the function uses Application.WorksheetFunction.Min on it's own and if 0 is returned then uses Application.WorksheetFunction.Small to find the next minimum above 0
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Re: Excel help needed

Postby Ian » Thu Mar 25, 2010 12:29 pm

alrodopial wrote:Hi,
I want to find the minimum of a range with criteria using only VBA.

So the min of A1:A100 if all prices at this range are above 0 (>0 the criteria) with VBA
If it was a formula:
MIN(IF(A1:A100>0),A1:A100)) array formula

Any ideas?

I can loop through each cell in the range but it's a slow VBA this way



All the prices in the range are > 0 is how I read it.
Ian
 
Posts: 834
Joined: Sat Nov 19, 2005 8:35 am
Location: Birmingham

Re: Excel help needed

Postby osknows » Thu Mar 25, 2010 12:32 pm

alrodopial wrote:Hi,
I want to find the minimum of a range with criteria


I read this bit.. :)
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby Ian » Thu Mar 25, 2010 12:47 pm

Is there a reason why you couldn't have used ....SMALL on its own ?

I've got that formula in a couple of spreadsheets (not vba though) and it
always finds the minimum OK even if none of the cells pass the criteria as you end up with 1 for the n-th position.
Ian
 
Posts: 834
Joined: Sat Nov 19, 2005 8:35 am
Location: Birmingham

Postby osknows » Thu Mar 25, 2010 1:32 pm

I suppose for what it's being used for then using SMALL is fine. The calculation time for SUMIF and COUNTIF are proportionate to the number of cells covered, so over large ranges the MIN function is quicker, hence why it's used first.
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby alrodopial » Thu Mar 25, 2010 1:59 pm

Thanks os, I'll give it a try later tonight.
Ian you are right, the way I wrote it wasn't clear enough (English ....)
I meant the smallest of the range and at the same time larger than zero.

Thanks guys
alrodopial
 
Posts: 1384
Joined: Wed Dec 06, 2006 9:59 pm

Postby alrodopial » Sat Mar 27, 2010 3:18 pm

It's working fine,
Thanks
alrodopial
 
Posts: 1384
Joined: Wed Dec 06, 2006 9:59 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 61 guests

Sports betting software from Gruss Software


The strength of Gruss Software is that it’s been designed by one of you, a frustrated sports punter, and then developed by listening to dozens of like-minded enthusiasts.

Gruss is owned and run by brothers Gary and Mark Russell. Gary discovered Betfair in 2004 and soon realised that using bespoke software to place bets was much more efficient than merely placing them through the website.

Gary built his own software and then enhanced its features after trialling it through other Betfair users and reacting to their improvement ideas, something that still happens today.

He started making a small monthly charge so he could work on it full-time and then recruited Mark to help develop the products and Gruss Software was born.

We think it’s the best of its kind and so do a lot of our customers. But you can never stand still in this game and we’ll continue to improve the software if any more great ideas emerge.