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 ?
 
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
Did you see the code Fluff posted in Message #7 yet? And if XL2019 has the MINIFS function, Peter's code in Message #8?
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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

An small change needed the sentence to work :
The parenthesis between the MIN and the IF was missing and the letter "s" had to be removed after the IF

This code its working properly:
VBA Code:
Evaluate("min(if(d33:m33,d33:m33,""<>0""))")
 
Upvote 0
Did you see the code Fluff posted in Message #7 yet?

Code in Message #7 its working properly in XL2019:

VBA Code:
Sub Test4()
MsgBox [min(if(a1:a100<>0,a1:a100))]
End Sub



And if XL2019 has the MINIFS function, Peter's code in Message #8?

Code in Message #8, It was not working :

VBA Code:
Sub Test5()
MsgBox = Evaluate("minifs(a1:a100,a1:a100,""<>0"")")
End Sub

An small change needed the sentence to work :
The parenthesis between the MIN and the IF was missing and the letter "s" had to be removed after the IF
And now, the code works properly in XL2019

VBA Code:
Evaluate("min(if(a1:a100,a1:a100,""<>0""))")
 
Upvote 0
An small change needed the sentence to work :
The parenthesis between the MIN and the IF was missing and the letter "s" had to be removed after the IF
It shouldn't need that change as MINIFS is a valid function in Excel 2019 according to Microsoft's help on this function.
The code certainly worked for me as I posted it.
However, if you have something that you are happy with, that is fine. :)

1588294511934.png
 
Upvote 0
It shouldn't need that change as MINIFS is a valid function in Excel 2019 according to Microsoft's help on this function.
The code certainly worked for me as I posted it.
However, if you have something that you are happy with, that is fine. :)

View attachment 12765

It doesn`t work for me the "MINIFS" function.

But with the change that i did and i`ve posted in message #13; it works.

Have you run the code with function MINIFS? Or are just just being guided by the manual help of Microsoft office?.
 
Upvote 0
Have you run the code with function MINIFS?
Yes but really No. I originally had a variable equal to the evaluate part of that code and viewed the result. However, to post the code I decided to go straight to the MsgBox and forgot to remove the "=" sign and must not have tested, sorry. :oops:
Just remove that "=" sign and try again. (I have tested this time. :))

VBA Code:
Sub Test5()
  MsgBox Evaluate("minifs(a1:a100,a1:a100,""<>0"")")
End Sub
 
Upvote 0
Yes but really No. I originally had a variable equal to the evaluate part of that code and viewed the result. However, to post the code I decided to go straight to the MsgBox and forgot to remove the "=" sign and must not have tested, sorry. :oops:
Just remove that "=" sign and try again. (I have tested this time. :))

VBA Code:
Sub Test5()
  MsgBox Evaluate("minifs(a1:a100,a1:a100,""<>0"")")
End Sub

When i try put this code for example in a variable X or with the funcion MsgBox, vba show me an error: "Run-Time error '13': Type Mismatch.

VBA Code:
x=Evaluate("minifs(a1:a100,a1:a100,""<>0"")")

VBA Code:
MsgBox Evaluate("minifs(a1:a100,a1:a100,""<>0"")")





But when i try put this another code in the same variable X or with function MsgBox, the code works fine.

VBA Code:
x=Evaluate("min(if(a1:a100,a1:a100,""<>0""))")
VBA Code:
MsgBox Evaluate("min(if(a1:a100,a1:a100,""<>0""))")
 
Upvote 0
Yes but really No. I originally had a variable equal to the evaluate part of that code and viewed the result. However, to post the code I decided to go straight to the MsgBox and forgot to remove the "=" sign and must not have tested, sorry. :oops:
Just remove that "=" sign and try again. (I have tested this time. :))

VBA Code:
Sub Test5()
  MsgBox Evaluate("minifs(a1:a100,a1:a100,""<>0"")")
End Sub

Sorry, I had tested it on another pc with XL2016 and that was the reason why it didn't work.

Now i ran the code in XL2019 and its works!. Thank you very much.
 
Upvote 0
Sorry, I had tested it on another pc with XL2016 and that was the reason why it didn't work.
Pleased to see that it wasn't only me making errors in this thread. :)
Glad that you got it working in the end. (y)
 
Upvote 0

Forum statistics

Threads
1,221,517
Messages
6,160,260
Members
451,635
Latest member
nithchun

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