+ Reply to Thread
Results 1 to 6 of 6

Get maximum value for unique record in three column

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2015
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    85

    Get maximum value for unique record in three column

    Dear All,

    I have data from column A to F.

    For each unique record Row wise in Column "B". "E" and "F" I want max value in Column "C".

    Kindly go through attached file in which I have shown "Data" sheet and "REQUIRED RESULT" sheet. I wish to have solution using VBA. In "DATA" Sheet In "H" to "N" Column I have shown how logic goes.

    Regards,

    PN
    Attached Files Attached Files

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Get maximum value for unique record in three column

    Hi PN,

    Try this:

    Sub pnnaik(): Dim Key As String, R As Range, M As Long, F As Range
    Dim i As Long, j As Long, k As Long, CEF As String: i = 2: k = 2
    [H1] = "": Key = [B2] & "~" & [E2] & "~" & [F2]: CEF = Key: j = i
    Doit: Do Until CEF <> Key: j = j + 1:
    CEF = Cells(j, 2) & "~" & Cells(j, 5) & "~" & Cells(j, 6)
    Loop
    Set R = Range("C" & i & ":C" & j - 1)
    M = WorksheetFunction.MAX(R): Set F = R.Find(M)
    Range("I" & k).Resize(1, 6).value = Range("A" & F.row).Resize(1, 6).value
    Range("I" & k).Resize(1, 6).BorderAround Weight:=xlThin
        Key = Cells(j, 2) & "~" & Cells(j, 5) & "~" & Cells(j, 6)
                        If Cells(j, 2) <> "" Then
                    i = j: k = k + 1: GoTo Doit: End If
                            i = 2: j = i: Key = [J2]
    Groupit:            Do Until Cells(j, 10) <> Key: j = j + 1: Loop
                        Set R = Range("H" & i & ":H" & j - 1): R.Merge
                        R.Resize(j - i, 7).BorderAround Weight:=xlMedium
                        R = "Record for " & Key: Key = Cells(j, 10): i = j
                        If Cells(j, 10) = "" Then GoTo ExitSub
                        GoTo Groupit
    ExitSub:  Set R = Union(Range("J2:J" & j), Range("M2:M" & j), Range("N2:N" & j))
                R.Interior.COLOR = [B2].Interior.COLOR
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    01-17-2015
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    85

    Re: Get maximum value for unique record in three column

    Dear xladept,

    This is perfect. Thanks for kind reply and solution. If you spare few minutes and provide short explanation on this code we
    can learn from you "Gurus".

    Also Column "H" record ( like Record for X1), I show for expiation, do not required that column.


    Regards,
    PN

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Get maximum value for unique record in three column

    Hi PN,

    Thanks for the rep!

    Sub pnnaik(): Dim Key As String, R As Range, M As Long, F As Range
    Dim i As Long, j As Long, k As Long, CEF As String: i = 2: k = 2
                'Clear H1, Join B,E,F for use as key
    [H1] = "": Key = [B2] & "~" & [E2] & "~" & [F2]: CEF = Key: j = i
                'Determine where key changes
    Doit: Do Until CEF <> Key: j = j + 1:
                'Use CEF as interim key for comparison
    CEF = Cells(j, 2) & "~" & Cells(j, 5) & "~" & Cells(j, 6)
    Loop
            'Define the range to inspect for a maximum value
    Set R = Range("C" & i & ":C" & j - 1)
            'Find the maximum value and the range it occupies
            M = WorksheetFunction.MAX(R): Set F = R.Find(M)
        'Write those values to the kth row (starting with 2) from the Ith column
    Range("I" & k).Resize(1, 6).value = Range("A" & F.row).Resize(1, 6).value
        'Put a thin border around that output range
    Range("I" & k).Resize(1, 6).BorderAround Weight:=xlThin
            'Reinitialize Key for next comparison
        Key = Cells(j, 2) & "~" & Cells(j, 5) & "~" & Cells(j, 6)
                'If data continues then
                        If Cells(j, 2) <> "" Then
                    'reset indices and increment output row index k
                            'and repeat procedure
                    i = j: k = k + 1: GoTo Doit: End If
                    'Reset indices to start again from the top
                            'Key on just the ID
                            i = 2: j = i: Key = [J2]
                            'Determine where Key (ID) changes
    Groupit:            Do Until Cells(j, 10) <> Key: j = j + 1: Loop
                            'Define the range to outline
                        Set R = Range("I" & i & ":I" & j - 1)
                            'Outline the range for that ID
                        R.Resize(j - i, 6).BorderAround Weight:=xlMedium
                            'Reset Key and row index for next grouping
                             Key = Cells(j, 10): i = j
                             'If data at end jump to ending procedure
                        If Cells(j, 10) = "" Then GoTo ExitSub
                        'Else repeat
                        GoTo Groupit
                        'Add colors to the three columns and finish
    ExitSub:  Set R = Union(Range("J2:J" & j), Range("M2:M" & j), Range("N2:N" & j))
                R.Interior.COLOR = [B2].Interior.COLOR
    End Sub

  5. #5
    Registered User
    Join Date
    01-17-2015
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    85

    Re: Get maximum value for unique record in three column

    Dear xladept,

    Thanks for your kind help.

    Regards,
    PN

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Get maximum value for unique record in three column

    You're welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to extract maximum record from dataset based on multiple criteria?
    By adelkam in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-15-2013, 04:41 AM
  2. Replies: 4
    Last Post: 12-17-2012, 10:14 AM
  3. [SOLVED] How to get a record in column A with unique value in column B ?
    By Mirrco in forum Excel General
    Replies: 4
    Last Post: 11-22-2012, 03:25 AM
  4. [SOLVED] vba to get the minimum and maximum values based on a unique ID
    By alexbotea2005 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2012, 02:44 PM
  5. Replies: 0
    Last Post: 01-17-2011, 07:13 AM
  6. Getting the Maximum Record # in Access
    By accessdanwi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-28-2008, 09:05 AM
  7. Next Unique Maximum
    By kwiklearner in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2006, 02:29 PM

Tags for this Thread

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