Finding lowest non zero value in excel VBA

raj

New Member
Joined
Mar 12, 2002
Messages
4
How can i get the lowest non zero value among a set of integers within excel VBA ?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
On 2002-05-20 09:25, raj wrote:
How can i get the lowest non zero value among a set of integers within excel VBA ?

You did not mention negative numbers assuming these are allowed the following will work.

Code:
Sub FindLowest()
IamTheLowest = 9E+50
LowAddr = ""
For Each c In Selection.Cells
    cv = c.Value
    ca = c.Address
    If cv < IamTheLowest And cv <> 0 Then
        IamTheLowest = cv
        LowAddr = ca
    End If
Next
MsgBox "Lowest Value is: " & IamTheLowest & Chr(10) & "In cell address: " & LowAddr
End Sub

If Negative numbers are not allowed.
then this will do it.
Code:
Sub FindLowest()
IamTheLowest = 9E+50
LowAddr = ""
For Each c In Selection.Cells
    cv = c.Value
    ca = c.Address
    If cv < IamTheLowest And cv > 0 Then
        IamTheLowest = cv
        LowAddr = ca
    End If
Next
MsgBox "Lowest Value is: " & IamTheLowest & Chr(10) & "In cell address: " & LowAddr
End Sub

Hope this helps.
:)
 
Upvote 0
Another option...

Code:
Sub test()
Dim x, Rng1

Set Rng1 = Range("A1:A100")
x = WorksheetFunction.Min(Rng1)
If x = 0 Then
    x = Evaluate("=MIN(IF(" _
    & Rng1.Address & ">0," _
    & Rng1.Address & "))")
End If
MsgBox x
End Sub

Bye,
Jay
 
Upvote 0
Another option...

Code:
Sub test()
Dim x, Rng1

Set Rng1 = Range("A1:A100")
x = WorksheetFunction.Min(Rng1)
If x = 0 Then
    x = Evaluate("=MIN(IF(" _
    & Rng1.Address & ">0," _
    & Rng1.Address & "))")
End If
MsgBox x
End Sub

Bye,
Jay

Brilliant, Solution!.
 
Upvote 0
You have responded to an 18 year old thread. No matter, how about a one-liner...
Rich (BB code):
Sub Test()
  MsgBox Evaluate("MIN(" & Replace(Application.Trim(Replace(" " & Join(Evaluate("TRANSPOSE(A1:A100)")) & " ", " 0 ", " ")), " ", ",") & ")")
End Sub
 
Last edited:
Upvote 0
Or even shorter
VBA Code:
Sub Test4()
  MsgBox [min(if(a1:a100<>0,a1:a100))]
End Sub
:)
 
Upvote 0
Or even shorter
For typed characters, yes but for processes/time no. :)

This has a few more characters but nearly twice as fast for me.
VBA Code:
Sub Test5()
  MsgBox = Evaluate("minifs(a1:a100,a1:a100,""<>0"")")
End Sub
 
Upvote 0
MINIFS ? It looks like I am going to have to upgrade my version of Excel (eventually)... too many new functions have been introduced since XL2010.
 
Upvote 0
Rich (BB code):
Sub Test()
  MsgBox Evaluate("MIN(" & Replace(Application.Trim(Replace(" " & Join(Evaluate("TRANSPOSE(A1:A100)")) & " ", " 0 ", " ")), " ", ",") & ")")
End Sub

very good short and precise solution


You have responded to an 18 year old thread. No matter, how about a one-liner...

As old as the post is, as long as it is active and it helps users to know if a code line worked or not, it will always be useful.

MINIFS ? It looks like I am going to have to upgrade my version of Excel (eventually)... too many new functions have been introduced since XL2010.

its about time?... to update the software. Office is already with the XL2019 version.
 
Upvote 0

Forum statistics

Threads
1,221,507
Messages
6,160,219
Members
451,631
Latest member
coffiajoseph

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top