-1

I have an array:- Dim arr(0 To 5, 1 To 2) As Variant

I am able to find the minimum value in the array using:-

Debug.Print Application.Min(arr)

How do I find the values that are populated adjacent to the minimum value?

Anthony
  • 542
  • 1
  • 8
  • 25
  • The same way you would with 5x2 cells or 2x5 cells on the worksheet. –  Mar 28 '18 at 06:53
  • @Jeeped I am confused! The position of the minimum value changes so I do not know how extract the positions adjacent. – Anthony Mar 28 '18 at 06:57
  • @Jeeped I tried this:- p = Application.Match(Application.Min(arr), arr, 0) Debug.Print p To find the position of the minimum value but this gives me an error. – Anthony Mar 28 '18 at 07:00

1 Answers1

0

Here is logic for one way ( note I am reading the array in from the sheet rather than having it already specified).

I loop columns as there are less columns than rows. I seed an initial valid minimum value by taking column 1 minimum Application.WorksheetFunction.Min(Application.WorksheetFunction.Index(arr, 0, 1)) . I set this value to that of a future column minimum if that future minimum is less than the existing.

Application.WorksheetFunction.Index is used to slice the array. By specifying Index(array,0,1) I am slicing column wise as opposed to Index(array,1,0) which would be row wise. There are limitations on the array dimensions that will be handled by Index.

Option Explicit

Public Sub GetMinumum()

Dim arr()

arr = ThisWorkbook.Worksheets("Sheet3").Range("A1").CurrentRegion.Value

Dim currColumn As Long

Dim currMinimum As Double

'set starting value for comparison
currMinimum = Application.WorksheetFunction.Min(Application.WorksheetFunction.Index(arr, 0, 1))

For currColumn = LBound(arr, 2) To UBound(arr, 2)

    If Application.WorksheetFunction.Min(Application.WorksheetFunction.Index(arr, 0, currColumn)) < currMinimum Then

      currMinimum = Application.WorksheetFunction.Min(Application.WorksheetFunction.Index(arr, 0, currColumn))

    End If

Next currColumn

Debug.Print currMinimum

End Sub

Output is -3

Data

Version 2 to get adjacent value

Option Explicit

Public Sub GetMinumum()

    Dim arr()

    arr = ThisWorkbook.Worksheets("Sheet3").Range("A1").CurrentRegion.Value

    Dim currRow As Long

    Dim currMinimum As Double
    currMinimum = arr(1, 1)
    Dim outputValue As Variant
    outputValue = vbNullString

    For currRow = LBound(arr, 1) To UBound(arr, 1)

        If arr(currRow, 1) < currMinimum Then
            currMinimum = arr(currRow, 1)
            outputValue = arr(currRow, 2)
        End If
    Next currRow

    Debug.Print outputValue

End Sub
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • appreciate your help but your answer is too complex for me! Not sure that I understand it. I am a newbie. I am able to find the minimum value in my array which in your example would be -3 but I want to how, once I have found the minimum value, to return the value associated with it in Col B. In your example it is 8. – Anthony Mar 28 '18 at 10:57
  • See my edit version 2. You have an array in which you loop the arr(currRow,1) which is the each row column 1 (i.e. A) and you find the minimum and retrieve the second column , arr(currRow,2) , value to get what is in column B – QHarr Mar 28 '18 at 11:09
  • 1
    Thank you for your patience! I have now had my aha moment and have got it. you are a scholar and a gentleman. – Anthony Mar 28 '18 at 11:39