ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to Question (https://www.excelbanter.com/excel-programming/376594-how-question.html)

Sandy

How to Question
 
I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you

Charles Chickering

How to Question
 
Sandy, I can do this with a loop but I'm not sure if this is the best way.
Dim dMin As Double
Dim lPostion As Long
Dim cnt As Long
dMin = SandyArr(LBound(SandyArr))
lPosition = LBound(SandyArr)
For cnt = LBound(SandyArr) to UBound(SandyArr)
If SandyArr(cnt) < dMin Then
dMin = SandyArr(cnt)
lPosition = cnt
End If
Next
MsgBox "The lowest value in Sandy's array is: " & dMin & _
vbCrLF & "And is in position: " & lPosition
--
Charles Chickering

"A good example is twice the value of good advice."


"Sandy" wrote:

I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you


Sandy

How to Question
 
Thank you. How can I find the lowest non zero (< 0) value and its position
in the array.


"Charles Chickering" wrote:

Sandy, I can do this with a loop but I'm not sure if this is the best way.
Dim dMin As Double
Dim lPostion As Long
Dim cnt As Long
dMin = SandyArr(LBound(SandyArr))
lPosition = LBound(SandyArr)
For cnt = LBound(SandyArr) to UBound(SandyArr)
If SandyArr(cnt) < dMin Then
dMin = SandyArr(cnt)
lPosition = cnt
End If
Next
MsgBox "The lowest value in Sandy's array is: " & dMin & _
vbCrLF & "And is in position: " & lPosition
--
Charles Chickering

"A good example is twice the value of good advice."


"Sandy" wrote:

I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you


Gary Keramidas

How to Question
 
how about this? it sorts the array and the lowest number will be arr(0)

modified from someone's code here


Sub sort_array()
Dim arr As Variant
Dim i As Long, j As Long, temp As Long
'sort the array
arr = Array(2, 3, 4, 1, 6, 8,7)
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) arr(j) Then
temp = arr(j)
arr(j) = arr(i)
arr(i) = temp
End If
Next j
Next i
MsgBox arr(0)
End Sub

--


Gary


"Sandy" wrote in message
...
I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you




Gary Keramidas

How to Question
 
sandy:

if you use the method i posted, it would be contained in arr(1), assuming there
is a zero in the array. are there negative numbers?

try it with your numbers and let me know what happens

--


Gary


"Sandy" wrote in message
...
Thank you. How can I find the lowest non zero (< 0) value and its position
in the array.


"Charles Chickering" wrote:

Sandy, I can do this with a loop but I'm not sure if this is the best way.
Dim dMin As Double
Dim lPostion As Long
Dim cnt As Long
dMin = SandyArr(LBound(SandyArr))
lPosition = LBound(SandyArr)
For cnt = LBound(SandyArr) to UBound(SandyArr)
If SandyArr(cnt) < dMin Then
dMin = SandyArr(cnt)
lPosition = cnt
End If
Next
MsgBox "The lowest value in Sandy's array is: " & dMin & _
vbCrLF & "And is in position: " & lPosition
--
Charles Chickering

"A good example is twice the value of good advice."


"Sandy" wrote:

I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you




Sandy

How to Question
 
Thank you very much for your advice and time. I took your original suggestion
and embedded it in my routine and added a couple if statements to "filter
out" unwanted results and it works great. Thanks again.

"Gary Keramidas" wrote:

how about this? it sorts the array and the lowest number will be arr(0)

modified from someone's code here


Sub sort_array()
Dim arr As Variant
Dim i As Long, j As Long, temp As Long
'sort the array
arr = Array(2, 3, 4, 1, 6, 8,7)
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) arr(j) Then
temp = arr(j)
arr(j) = arr(i)
arr(i) = temp
End If
Next j
Next i
MsgBox arr(0)
End Sub

--


Gary


"Sandy" wrote in message
...
I have an array with let's say 10 numeric values. How can I finf the lowest
non zero value and its position in the array.

Thank you






All times are GMT +1. The time now is 03:10 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com