Can I use MIN with a 2 Dim Array?

dlowrey

New Member
Joined
May 31, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have created and populated a 2-dimension array.
I think of arrays as rows and columns. aDistance(1,1) is a Name, aDistance(1,2) is a distance
I don't know how to reference all of the values in the second column of the array.

Dim aDistance() As Varient
ReDim aDistance(Last_Row, 2) 'Last_Row is variable of a range of rows in a worksheet, code not included here for clarity
The array is fully populated dynamically by other vba code. The resulting sample data set has 34 rows.

I want to obtain the minimum distance from all of the values in the Distance column of the array.
I am hoping the below will work, IF I knew how to reference the range of values in the second column.
mClosestPt = Min(aDistance(something here?))

Thank you for your time.
-DL
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Give this a try:

VBA Code:
mClosestPt = Application.WorksheetFunction.Min(Application.WorksheetFunction.Index(aDistance, 0, 2))
 
Upvote 0
Here is an example. The result from running the code returns 3.

Book1.xlsx
IJ
5A3
6B40
7C500
Report 2


VBA Code:
Sub MINTD()
Dim AR() As Variant:    AR = Selection.Value2

With Application
    Debug.Print .WorksheetFunction.Min(.Index(AR, 0, 2))
End With
End Sub
 
Upvote 0
The above 2 posts show how to use Index to reference a single row or column of an array. But if your first column is all text values, you can apply the Min function to the whole array:

VBA Code:
Debug.Print WorksheetFunction.Min(aDistance)

since Min ignores non-numeric values.
 
Upvote 0
Solution
Wowie Eric W!
That is fantastic. I spent hours trying to figure this out and you did it beautifully.
Thank you.
-DL
 
Upvote 0
Glad we could help! :biggrin:

Keep the Index idea in mind though, sometimes you might have an array with multiple numeric columns.
 
Upvote 0
The above 2 posts show how to use Index to reference a single row or column of an array. But if your first column is all text values, you can apply the Min function to the whole array:
I got so focused on the question that I forgot to solve the problem :LOL:
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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