+ Reply to Thread
Results 1 to 7 of 7

Sorting "apples" and "bananas" from one list into two separate lists?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    3

    Sorting "apples" and "bananas" from one list into two separate lists?

    Hi,
    I have a master list which contains only "apples" and "bananas" in a mixed order in the first column, and the associated data (e.g. kilograms) in the second column.
    I would like to sort the data from this master list and make two separate lists, one with the "apples" data and one with the "bananas" data. I use Excel 2003. Any suggestions would be greatly appreciated.
    Please see the attached sample file. Thanks.
    Attached Files Attached Files
    Last edited by lonely707; 01-28-2012 at 02:54 PM.

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Sorting "apples" and "bananas" from one list into two separate lists?

    lonely707,


    Welcome to the Excel Forum.


    Detach/open workbook DistributeGroups w1 AB - lonely707 - EF811505 - SDG10.xls and run the DistributeGroups macro.


    If you want to use the macro on another workbook:


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    
    Option Explicit
    Sub DistributeGroups()
    ' stanleydgromjr, 01/26/2012
    ' http://www.excelforum.com/excel-general/811505-sorting-apples-and-bananas-from-one-list-into-two-separate-lists.html
    Dim w1 As Worksheet, wC As Worksheet
    Dim lr As Long, lrc As Long, r As Long, nc As Long
    Application.ScreenUpdating = False
    Set w1 = Worksheets("Sheet1")
    w1.Rows(1).Insert
    w1.Range("A1:B1") = [{"A","B"}]
    lr = w1.Range("A" & Rows.Count).End(xlUp).Row
    Set wC = Worksheets.Add
    w1.Range("A1:A" & lr).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wC.Range("A1"), Unique:=True
    lrc = wC.Range("A" & Rows.Count).End(xlUp).Row
    nc = 5
    For r = 2 To lrc Step 1
      w1.Range("A1:B" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=wC.Range("A1:A2"), CopyToRange:=w1.Cells(1, nc), Unique:=False
      wC.Rows(2).Delete
      nc = w1.Cells(1, Columns.Count).End(xlToLeft).Column + 2
    Next r
    Application.DisplayAlerts = False
    wC.Delete
    Application.DisplayAlerts = True
    w1.Rows(1).Delete
    Application.ScreenUpdating = True
    End Sub

    Then run the DistributeGroups macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  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: Sorting "apples" and "bananas" from one list into two separate lists?

    This little macro will split your two column data set into as many columns as you have unique values, not just two.

    Option Explicit
    
    Sub SplitLists()
    Dim vFIND As Range, vRNG As Range, v As Range
    
    Set vRNG = Range("A:A").SpecialCells(xlConstants)
    On Error Resume Next
    
    For Each v In vRNG
        Set vFIND = Range("D1:IV1").Find(v, LookIn:=xlValues, LookAt:=xlWhole)
        If vFIND Is Nothing Then
            Set vFIND = Cells(1, Columns.Count).End(xlToLeft).Offset(, 2)
            vFIND.Resize(, 2).Value = v.Resize(, 2).Value
        Else
            Cells(Rows.Count, vFIND.Column).End(xlUp).Offset(1).Resize(, 2) = v.Resize(, 2).Value
            Set vFIND = Nothing
        End If
    Next v
    
    End Sub

    I've added it to your sample workbook with a button. Just clear the sheet and drop in your real data, then run the macro.
    Attached Files Attached Files
    _________________
    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
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Sorting "apples" and "bananas" from one list into two separate lists?

    Jerry,

    Nice. One for my archives.

    Thanks.

  5. #5
    Registered User
    Join Date
    01-17-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Sorting "apples" and "bananas" from one list into two separate lists?

    Hey guys, thank you so much for the quick reply! That was extremely helpful. You are geniuses !!! Both solutions are just great.
    Thanks once again for your time and help, I really appreciate it !!!

    Marko

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

    Re: Sorting "apples" and "bananas" from one list into two separate lists?

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

  7. #7
    Forum Contributor Charlie_Howell's Avatar
    Join Date
    09-27-2010
    Location
    Memphis, TN
    MS-Off Ver
    Excel 2007
    Posts
    218

    Re: Sorting "apples" and "bananas" from one list into two separate lists?

    Take a look at this approach as well and let us know if this could work for you.
    There is no substitute for clean data.

+ 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