Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Finding highest value in array

  1. #11
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    ExcelGeek
    Not sure how to use the code example you show here - what does aplication.large return
    Last edited by Rasm; 06-18-2011 at 10:26 AM. Reason: typo
    xl2007 - Windows 7
    xl hates the 255 number

  2. #12
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    Excelgeek
    I got this working - so that is cool - anyway I can get the coordinates of the elements as well - so I know the position in the array where it found the ranked value?

    It seems that the WorksheetFunction.Match is the one to use - to find the position in the array - But my array is 20 by 20 - so I cannot get Match to work


    Code:
    Avar=Application.WorksheetFunction.Large(CorrelMatrix, 1)
    Last edited by Rasm; 06-18-2011 at 08:13 PM.
    xl2007 - Windows 7
    xl hates the 255 number

  3. #13
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0
    Assuming a 1-based array, you could use:
    Code:
    for n = lbound(correlmatrix, 2) to ubound(correlmatrix, 2)
    varmatch = application.match(avar, application.index(correlmatrix, 0, n))
    if not iserror(varmatch) then
       msgbox "x=" & varmatch & "; y=" & n
    end if
    next n
    though I suspect it would be as fast if not faster to simply iterate every element rather than using worksheet functions on arrays.

  4. #14
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    Excelgeek
    I will try it - but I think you are right about simply using a loop and find it that way instead - it may be faster with the loop - thanks.
    xl2007 - Windows 7
    xl hates the 255 number

  5. #15
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Rasm,

    It's not a bad idea to write the array in a worksheet, use some formulas and get back the results in an array.

    Here is a routing which might help you.

    Code:
    Function SORTMATRIX(ByRef Matrix, ByRef ObvsDown, ByRef ObvsAcross) As Variant
        
        Dim UB1         As Long
        Dim UB2         As Long
        Dim strMatrix   As String
        Dim strObvsD    As String
        Dim strObvsA    As String
        Dim strMaxVals  As String
        Dim strRank     As String
        
        Dim wksTemp     As Worksheet
        
        With Application
            .ScreenUpdating = 0
            .DisplayAlerts = 0
        End With
        
        If TypeOf Matrix Is Range Then Matrix = Matrix.Value
        If TypeOf ObvsDown Is Range Then ObvsDown = ObvsDown.Value
        If TypeOf ObvsAcross Is Range Then ObvsAcross = ObvsAcross.Value
        
        
        Set wksTemp = ThisWorkbook.Worksheets.Add
        
        UB1 = UBound(Matrix, 1)
        UB2 = UBound(Matrix, 2)
        
        With wksTemp
            .Range("b2").Resize(UB1, UB2).Value = Matrix
            .Range("a2").Resize(UB1).Value = ObvsDown
            .Range("b1").Resize(, UB2).Value = ObvsAcross
            .Range("a:c").EntireColumn.Insert
            strMatrix = .Range("e2").Resize(UB1, UB2).Address
            strObvsD = .Range("d2").Resize(UB1).Address
            strObvsA = .Range("e1").Resize(, UB2).Address
            strMaxVals = .Range("b2").Resize(UB1).Address
            strRank = .Range("c2").Resize(UB1).Address
            
            .Range("b2").Resize(UB1).Formula = "=MAX(INDEX(" & strMatrix & ",0,MATCH(d2," & strObvsA & ",0)))"
            .Range("a2").Resize(UB1).Formula = "=INDEX(" & strObvsD & ",MATCH(b2,INDEX(" & strMatrix & ",0,MATCH(d2," & strObvsA & ",0)),0))"
            .Range("c2").Resize(UB1).Formula = "=RANK(B2," & strMaxVals & ")+COUNTIF($B$2:B2,B2)-1"
            
            With .Range("a2").Resize(UB1, 3)
                .Value = .Value2
                .Sort .Cells(1, 2), 2, Header:=2
                SORTMATRIX = .Cells(1).Resize(UB1, 2)
            End With
        End With
        wksTemp.Delete
        
        With Application
            .ScreenUpdating = 1
            .DisplayAlerts = 1
        End With
        
    End Function
    and call

    Code:
    Sub kTest()
        
        Dim a
        
        a = SORTMATRIX([b2:u21], [a2:a21], [b1:u1])'the range could be CorrelMatrix
        
    End Sub
    Last edited by Admin; 06-21-2011 at 09:27 PM.

  6. #16
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    Been away - just seen this - I will try it - thanks
    xl2007 - Windows 7
    xl hates the 255 number

Similar Threads

  1. Find the highest then lookup result
    By Stalker in forum Excel Help
    Replies: 4
    Last Post: 04-02-2013, 02:04 PM
  2. finding the number of occurrence
    By zzzqinzzz in forum Excel Help
    Replies: 2
    Last Post: 12-13-2012, 10:24 AM
  3. Replies: 5
    Last Post: 09-26-2012, 10:17 AM
  4. Finding Credit and Debit Data and Creating Pivot
    By Prabhu in forum Excel Help
    Replies: 10
    Last Post: 01-29-2012, 11:03 PM
  5. Finding Last Used Row or Column In Excel Sheet
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 04-14-2011, 03:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •