Closed Thread
Results 1 to 22 of 22

match and sort by two columns

Hybrid View

  1. #1
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    match and sort by two columns

    Column A has customer name, columns B,C,D has corresponding sales data for
    that month. Column E is blank. Column F has has customer name. Column
    G,H,I has the same corresponding sales data for the next month.

    Although Column A and F both have customer names, there are many changes
    month over month with the addition and deletion of certain customers so the
    rows do not match up exactly. Is there a quick way to have the rows match up
    according to customer names in column A and F and then sort them
    alpabetically?

    Currently my list looks like this:
    Jan Feb
    A A
    B C
    D D
    F E
    H F
    L G
    M H

    I would like it to look like this:
    Jan Feb
    A A
    B
    C
    D D
    E
    F F
    G
    H H
    L
    M

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: match and sort by two columns

    post a workbook!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Hi Yowzer, welcome to the forum.

    Click GO ADVANCED and use the paperclip icon to post up your sample workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    Basically, I want the rows lined up by what is in columns A and M. There are some entries that are in A that are not in M, so the M through W would be blank in that row. There are some entries in M that are not in A, so A through K would be blank in that row. I hope this is clear for what I am looking to do. I appreciate your help.
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Try this on a copy of your sheet:
    Option Explicit
    
    Sub LineThemUp()
    'JBeaucaire  2/10/10
    'Lines up 2nd table column M with 1st table column A values
    Dim LR As Long
    Application.ScreenUpdating = False
    
    Columns("A:A").Copy Range("Z1")
    
    LR = Range("M" & Rows.Count).End(xlUp).Row
    Range("M4:M" & LR).Copy Range("Z" & Rows.Count).End(xlUp).Offset(1, 0)
    
    Range("Z:Z").SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
    Range("Z:Z").Sort [Z1], xlAscending, , , , , , xlYes
    
    LR = Range("Z" & Rows.Count).End(xlUp).Row
    Range("Z1:Z" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("L3"), Unique:=True
    Range("Z:Z").Clear
    
    Dim i As Long:  i = 4
    Do
        Do While Cells(i, "A") = ""
            Range(Cells(i, "A"), Cells(i, "K")).Delete xlShiftUp
        Loop
            
        If Cells(i, "M") > Cells(i, "L") Then _
            Range(Cells(i, "M"), Cells(i, "W")).Insert xlShiftDown
        If Cells(i, "A") > Cells(i, "L") Then _
            Range(Cells(i, "A"), Cells(i, "K")).Insert xlShiftDown
        
        i = i + 1
    Loop Until Cells(i, "K") = ""
    
    Range("L:L").Clear
    Application.ScreenUpdating = True
    MsgBox "Complete"
    End Sub

  6. #6
    Registered User
    Join Date
    07-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: match and sort by two columns

    Can you explain this macro or its difficult to explain, Can you atleast explain the concept

  7. #7
    Registered User
    Join Date
    07-23-2013
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: match and sort by two columns

    I am trying to do the same thing with my workbook, but am having difficulties. Could you show me how? I have attached the relevant file. (I am matching Columns A & B; C is data related to Column B).
    Thank you.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    wow thank you soooo much. you just saved me a couple hours of work every month.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

  10. #10
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    I will be sure to mark solved. I hate to ask again, but is this possible to do with 4 columns? Its pretty much the same exact thing as the previous example but with 4 tables that need to be aligned by columns B, O, AB, AL. This would be a life saver!!!
    Attached Files Attached Files

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Hehe...not exactly the same thing, but try this:
    Option Explicit
    
    Sub LineThemUp()
    'JBeaucaire  2/16/2010
    'Lines up values in columns B, O, AB and AL
    Dim LR As Long
    Application.ScreenUpdating = False
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    'Create a unique list of all values
    Range("B3:B" & LR).Copy Range("DD1")
    Range("O4:O" & LR).Copy Range("DD" & Rows.Count).End(xlUp).Offset(1, 0)
    Range("AB4:AB" & LR).Copy Range("DD" & Rows.Count).End(xlUp).Offset(1, 0)
    Range("AL4:AL" & LR).Copy Range("DD" & Rows.Count).End(xlUp).Offset(1, 0)
    
    Range("DD:DD").SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
    Range("DD:DD").Sort [DD1], xlAscending, , , , , , xlYes
    
    LR = Range("DD" & Rows.Count).End(xlUp).Row
    Range("DD1:DD" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("DE3"), Unique:=True
    Range("DD:DD").Clear
    
    'Filter out the blanks
    Range("A3:M" & LR).Sort Key1:=Range("B4"), Order1:=xlAscending, Header:=xlYes
    Range("N3:Z" & LR).Sort Key1:=Range("O4"), Order1:=xlAscending, Header:=xlYes
    Range("AA3:AJ" & LR).Sort Key1:=Range("AB4"), Order1:=xlAscending, Header:=xlYes
    Range("AK3:AT" & LR).Sort Key1:=Range("AL4"), Order1:=xlAscending, Header:=xlYes
    
    Dim i As Long:  i = 4
    Do
        If Cells(i, "B") > Cells(i, "DE") Then _
            Range(Cells(i, "A"), Cells(i, "M")).Insert xlShiftDown
        If Cells(i, "O") > Cells(i, "**") Then _
            Range(Cells(i, "N"), Cells(i, "Z")).Insert xlShiftDown
        If Cells(i, "AB") > Cells(i, "DE") Then _
            Range(Cells(i, "AA"), Cells(i, "AJ")).Insert xlShiftDown
        If Cells(i, "AL") > Cells(i, "DE") Then _
            Range(Cells(i, "AK"), Cells(i, "AT")).Insert xlShiftDown
        i = i + 1
    Loop Until Cells(i, "DE") = ""
    
    Range("DE:DE").Clear
    Application.ScreenUpdating = True
    MsgBox "Complete"
    End Sub

    Be patient, this one will take a while to loop.
    Last edited by JBeaucaire; 02-24-2010 at 04:38 PM.

  12. #12
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    Thanks so much for your time and help! I'm sorry, I got an error message stating "Type mismatch"

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Where in the code? You can speed up this process by providing more detail than that...or post up the misbehaving workbook with the macro already in it so I can see for myself.

  14. #14
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    thanks for all your help, ive posted the workbook
    Attached Files Attached Files

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    1) Delete the macro you downloaded from the ThisWorkbook module. The macro didn't go in there to begin with, and the forum corrupted the upload and put a bunch of ** in the code in place of column references I had used.

    Delete it.

    2) Click on INSERT > MODULE and post the code in a regular module. Download the code above again, I've repaired the corrupted code.


    Make sure you only put global workbook-event macros into ThisWorkbook, sheet-event macros into the sheet modules...all other macros go in regular modules.

  16. #16
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    sorry, im getting yet another error message. am i doing something wrong? it says

    Run-time error '1004':
    The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isnt the same or blank.
    Attached Files Attached Files

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Sorry, change this line near the top:

    Range("DD:DD").Sort [DD1], xlAscending, , , , , , xlYes

    And this near the bottom:
        If Cells(i, "O") > Cells(i, "DE") Then _
            Range(Cells(i, "N"), Cells(i, "Z")).Insert xlShiftDown

  18. #18
    Registered User
    Join Date
    02-03-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: match and sort by two columns

    Absolutely perfect. THanks for all your time!!!

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

  20. #20
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    As per a Private Message:

    Go to your original post, post #1 at the beginning of this whole thread.
    Click on EDIT
    Click on GO ADVANCED
    Next to the title is a drop box, set this prefix box to [SOLVED]


    ==========
    Across from my name in any post, this one included, are some icons. The Reputation Feedback icon is the one that looks like blue scales.

  21. #21
    Registered User
    Join Date
    07-21-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    12

    Smile Re: match and sort by two columns (New Query)

    Hi All,

    Can you resolve my excel and explain me the concept how to create macro, My problem is same thing of matching and sorting, but comparison of one column to many, for ex: Column A compare to B, C, D ..etc and sorting respective with column A. Is my problem is recognize by you. I have attached the excel for your reference.
    Attached Files Attached Files

  22. #22
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: match and sort by two columns

    Kay,

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

    ====================

Closed 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