Consulting

Results 1 to 5 of 5

Thread: Max Formula

  1. #1
    VBAX Regular
    Joined
    Dec 2008
    Posts
    22
    Location

    Smile Max Formula

    Hi
    I Know this is probably the wrong place to post this, but I have a quick question about formulae.

    I have a table which has two columns, an item name column and a quantity bought column. What I want to do is return the item name to the cell which has the maximum number on the quantity bought column. Is there any way to do this?

    Thanks
    Luke

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    =INDEX(A:A,MATCH(MAX(B:B),B1:B100,0))

    as an array formula, commit with Ctrl-Shift-Enter, not just Enter
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Dec 2008
    Posts
    22
    Location
    How can I customize this for my workbook? Could you explain each part of the formula?

    Thanks
    Luke

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Look up both INDEX & MATCH in the Help section.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Regular Mahahaava's Avatar
    Joined
    Feb 2008
    Location
    Lohja, Finland
    Posts
    26
    Location
    Hi,

    try looking at the attached xls -file. The following VBA -code is in Module FindMax in it(Run with Crtr+m):

    [vba]Option Explicit
    'This is a funtion to determine how many rows of data there is:
    Function LastUsedRow(WB As String, S As Variant) As Long
    'Find the last used Row on a ActiveSheet:
    Workbooks(WB).Worksheets(S).Activate
    Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Rows.
    LastRow = Cells.Find(What:="*", After:=[A1], _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    LastUsedRow = LastRow
    Else
    LastUsedRow = 0
    End If
    End Function

    Sub FindMaxValue()
    Dim MaxProd As String' The most sold productname
    Dim MaxQuantity As Long'The Max Quantity
    Dim Row As Integer'The row where the MaxQuantity lies
    Dim i As Integer'Running counter
    For i = 1 To LastUsedRow("MaxQuantity.xls", 1)
    If Worksheets("Sheet2").Cells(i, 2).Value > MaxQuantity Then
    'If the value for row "i" is larger than any before, then
    MaxQuantity = Worksheets("Sheet2").Cells(i, 2).Value
    'set the value of the Max to this new value and
    Row = i
    'remember the rownumber
    End If
    Next i
    MaxProd = Worksheets("Sheet2").Cells(Row, 1).Value
    'The most sold product is on row Row, column 1
    MsgBox "The most sold product is: " & MaxProd & ", " & _
    MaxQuantity & " sold."
    End Sub[/vba]
    HTH

    Petri

    Attachment 11182

Posting Permissions

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