+ Reply to Thread
Results 1 to 4 of 4

Problem using the Large function in VB

Hybrid View

Alf Problem using the Large... 05-24-2012, 05:41 PM
tigeravatar Re: Problem using the Large... 05-24-2012, 05:54 PM
tigeravatar Re: Problem using the Large... 05-24-2012, 06:03 PM
Alf Re: Problem using the Large... 05-25-2012, 04:04 PM
  1. #1
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Problem using the Large function in VB

    I use this macro to rank the 15 highest numbers in a range of values.

    Option Explicit
    
    Sub find_max()
    Dim MyRange As Range
    Dim i As Integer
    Dim j As Double
    
    Set MyRange = Range("D4:D" & Range("D" & Rows.Count).End(xlUp).Row)
    
    For i = 1 To 15
        j = Application.WorksheetFunction.Large(MyRange, i)
        MyRange.Find(What:=j, SearchOrder:=xlByRows).Activate
        ActiveCell.Offset(0, 1).Value = i
    Next i
    
    End Sub
    It works fine if the cell contain a value i.e. 18298.9 but if it’s a formula like "D4=C4*B4"
    I get “Run-time error ‘91’: Object variable or With block variable not set”

    I’ve tried to add “LookIn:=xlFormulas” and “LookAt:=xlPart” but I still get the same error message.

    Of course I could add a helper column converting formulas to values but I was hoping to find a solution as the Large function works with formulas in a “normal” Excel environment.

    Alf

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Problem using the Large function in VB

    Alf,

    I'm not sure why you wouldn't just use the formula, but here's a modified version of your macro. The .Find method needs to use xlValues to find values resulting from formulas. I also included a change to provide unique ranking just in case there are any duplicate numbers in the top 15:
    Sub find_max()
        
        Dim MyRange As Range
        Dim rngFound As Range
        Dim LargeIndex As Long
        Dim LargeValue As Long
        Dim LastVal As Long
        Dim OccurrenceNum As Long
        Dim i As Long
        
        Set MyRange = Range("D4", Cells(Rows.Count, "D").End(xlUp))
        
        For LargeIndex = 1 To 15
            LargeValue = Application.Large(MyRange, LargeIndex)
            If LargeValue <> LastVal Then
                MyRange.Find(LargeValue, , xlValues).Offset(, 1).Value = LargeIndex
                LastVal = LargeValue
                OccurrenceNum = 1
            Else
                OccurrenceNum = OccurrenceNum + 1
                Set rngFound = MyRange.Find(LargeValue, , xlValues)
                For i = 2 To OccurrenceNum
                    Set rngFound = MyRange.Find(LargeValue, rngFound, xlValues)
                Next i
                rngFound.Offset(, 1).Value = LargeIndex
            End If
        Next LargeIndex
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Problem using the Large function in VB

    Alf,

    Alternate, condensed version using the macro to input the formula, and then replacing the formula with its resulting values:
    Sub tgr()
        
        With Range("D4", Cells(Rows.Count, "D").End(xlUp))
            .Offset(, 1).Formula = "=IF(RANK(D4," & .Address & ")+COUNTIF($D$4:D4,D4)-1>15,"""",RANK(D4," & .Address & ")+COUNTIF($D$4:D4,D4)-1)"
            .Offset(, 1).Value = .Offset(, 1).Value
        End With
        
    End Sub

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: Problem using the Large function in VB

    Hi

    Thanks for help. I've used your second macro as it was short & compact.

    I'm not sure why you wouldn't just use the formula
    I wanted to place ranking number next to the appropriate value and did not manage to solve that just by using the formula.

    Regards

    Alf

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1