+ Reply to Thread
Results 1 to 3 of 3

Need to sort by matching columns

Hybrid View

  1. #1
    Registered User
    Join Date
    04-13-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question Need to sort by matching columns

    I have a two spreadsheets with a lot of data on them that I am trying to make sure have been transcribed correctly from one to the other through manual entry. I suspect this is not the case and I want to identify the errors and fix them. I took the relevant columns from each sheet and put them next to each other on a new one. Column A shows part number and Column B the corresponding quantity. Column C shows part number from the second sheet, and again Column D has the corresponding quantity. Unfortunately, there is a vast discrepancy in number of lines, as Column A has many more part numbers in it than Column C does.

    What I am attempting to accomplish is to have Excel find any exact matches between Columns A and C, and sort by that, while keeping the correct quantity data from Column B on the same row with its corresponding part number in Column A, and likewise for Columns D & C. Ideally, any rows that did not have a match, or where Column C was blank would be sorted to the bottom of the sheet.

    Attached is a small piece of the sheet I am dealing with for simplicity's sake. The full file has more than 10,000 rows. Any help would be greatly appreciated. Thank you.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Need to sort by matching columns

    Try this code

    Sub Macro7()
    
    LCA = Range("A65536").End(xlUp).Row
    LCC = Range("C65536").End(xlUp).Row
    
        Range("E1").FormulaR1C1 = "Matched QTY"
        
        Range("A2:D" & LCA).Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A" & LCA) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:D" & LCA)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        Range("E2:E" & LCC).FormulaR1C1 = "=IF(ISNA(MATCH(RC[-2],R2C1:R" & LCA - 1 & "C1,0)),""Not Found"",OFFSET(R1C[-4],MATCH(RC[-2],R2C1:R" & LCA - 1 & "C1,0),1))"
        Range("F2:F" & LCC).FormulaR1C1 = "=IF(RC[-1]=RC[-2],""Ok"",""Error"")"
        
            Range("A1:F" & LCC).Select
    
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F2:F" & LCC) _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A" & LCC) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:F" & LCC)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        Range("A!").Select
    End Sub

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Need to sort by matching columns

    Here's my interpretation of your problem.
    Attached Files Attached Files

+ 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