How to find the position of maximum value in VBA array ?

ajay1111

New Member
Joined
Aug 15, 2022
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have dynamic size of array and i want to find the position array(selected) which contains maximum value.
For example : Array size is 30 i.e Arr(30) and i want to find the position of maximum value among Arr(4), Arr(5) and Arr(6) only (Not all).

The code i tried does not work as intended as it gives position of maximum value of Arr(4), Arr(5) and Arr(6) among all arrays i.e Arr(30).
While I want position of maximum value of Arr(4), Arr(5) and Arr(6) among arrays Arr(4), Arr(5) and Arr(6) only.

Eg: If Arr(5) contains maximum value among Arr(4), Arr(5) and Arr(6) then "5" should be returned
VBA Code:
Sub posi()

  Dim position As Long
  
  position = WorksheetFunction.Match(WorksheetFunction.Max(Arr(1),Arr(2),Arr(3)), Arr(), 0)

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Maybe something like this (run this code on a new worksheet):
VBA Code:
Sub PositionOfMax()
Dim A(1 To 30), first, last, M, N As Long
'populate the array
For i = LBound(A) To UBound(A)
    A(i) = WorksheetFunction.RandBetween(1, 31)
Next i
' select start and end elements
first = 4: last = 6
'find max and position of max
For i = first To last
    If A(i) > M Then
        M = A(i)
        N = i
    End If
Next i
Range("A1:A30").Value = Application.Transpose(A)
MsgBox "Max value is :" & M & " in position " & N
End Sub
 
Upvote 0
Thank you for the help sir ! But i wanted to know if it is possible without using "loop" and just using worksheet function to achieve this !

Code:
maximumvalue=WorksheetFunction.Max(Arr(4),Arr(5),Arr(6))
position = WorksheetFunction.Match(maximumvalue, Arr(), 0)

Here, The position of maximumvalue is searched in all arrays: Arr(0) to Arr(30) due to use of Arr() in above code.
Is there a way search position of maximumvalue in Arr(4), Arr(5) & Arr(6) only by changing Arr() to something else !
 
Upvote 0
Don't think so. And, your position variable may not even correspond to one of the array elements among Arr(4), Arr(5), and Arr(6) if an earlier element [Arr(0) to Arr(3)] has the same maximumvalue since the Match function will find the relative position of the first matching element in Arr.
 
Upvote 0
But i wanted to know if it is possible without using "loop" and just using worksheet function to achieve this !
Try this (though I'm not sure it's any better than looping)

VBA Code:
Sub Position_Of_Max()
  Dim A(1 To 30), first As Long, last As Long, i As Long, Pos As Long, SubA As Variant
  
  'populate the array
  For i = LBound(A) To UBound(A)
      A(i) = WorksheetFunction.RandBetween(1, 31)
  Next i
  ' select start and end elements
  first = 4: last = 6
  
  'Extract Sub-Array
  SubA = Application.Index(A, Application.Transpose(Evaluate("row(" & first & ":" & last & ")")))
  
  'Calculate sub-array position and adjust to main array position
  Pos = WorksheetFunction.Match(WorksheetFunction.Max(SubA), SubA, 0) + first - 1
  
  MsgBox Pos
End Sub
 
Upvote 0
Solution
See if this does what you are after.

VBA Code:
Sub GetMaxPosInGivenRange()

    Dim rng As Range
    Dim arr As Variant
    
    Set rng = Range("A1:A14")
    arr = rng
    
    Dim position As Long
    Dim maximumvalue As Long    ' Change to Double if decimal
    
    maximumvalue = Application.Max(Application.Index(arr, Array(4, 5, 6), 0))
    position = Application.Match(maximumvalue, Application.Index(arr, Array(4, 5, 6), 0), 0)
    Debug.Print "Position on specified range: " & position

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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