MinOfArray

Gets the id of an item with minimum in an array of numbers, with option


Public

Tested

My Own Work
Function MinOfArray(Nums(), Optional OnlyPositives = 0)
' Gets the id of an item with minimum in an array of numbers, with option to get only positives
' returned id uses zero-based array
' OnlyPOsitives = 1 to get the minimum for positive numbers only
' OnlyPOsitives = 2 to get the minimum for negative numbers only
' OnlyPOsitives = 0 to get the minimum for all numbers
'
Rett = -1
Minn = 999999999 'Nums(0) ' assume 1st one is the min
X1 = 0
For Each Nuu In Nums()
GetMe = 0
If Nuu < Minn And Nuu >= 0 And OnlyPositives = 1 Then
GetMe = 1
ElseIf Nuu < Minn And Nuu < 0 And OnlyPositives = 2 Then
GetMe = 1
ElseIf Nuu < Minn And OnlyPositives = 0 Then
GetMe = 1
End If
If GetMe = 1 Then
Rett = X1
Minn = Nuu
End If
X1 = X1 + 1
Next
MinOfArray = Rett
End Function

Nums(), Optional OnlyPositives = 0

Sub TTT()
Dim Nu()
Nu = Array(1, 2, 3)
MsgBox MinOfArray(Nu, 1)
End Sub

Views 262 Downloads 38

VBA Math
ANmarAmdeen
655
Attachments
Revisions

v2.0