+ Reply to Thread
Results 1 to 7 of 7

Need macro that will group rows with duplicate data in a particular column together

  1. #1
    Registered User
    Join Date
    09-08-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    4

    Need macro that will group rows with duplicate data in a particular column together

    Here's what I'm working with...
    I have an excel spreadsheet with nearly 15,000 rows that I'm trying to sort. Each row has 68 columns, and represents a single piece of equipment that my company has shipped out to various job sites. The rows are sorted according to ship date, in descending order.

    Here's where it gets tricky...
    Sometimes as many as 30 separate pieces of equipment will be shipped out to a job site, but they're not all shipped on the same day. Because of that, the rows with the same value in the "Job Name" column aren't always grouped together, and instead are scattered throughout the spreadsheet.

    Here's what I'm trying to accomplish...
    What I need to do is group all equipment rows that belong to the same "Job Name" together. However, I must also keep those jobs sorted according to "Ship Date", with the most recent job at the top of my spreadsheet. Does anyone know how to create a macro that can do that?

    Here's an example of what the spreadsheet looks like now:

    {Equip Type, Job Name, Ship Date}

    CHILLER.....WESTSIDE MALL.....JAN 28
    AIR HND.....WESTSIDE MALL.....JAN 27
    CHILLER.....ELEM SCHOOL.........JAN 26
    AIR HND.....WESTSIDE MALL......JAN 25
    I-PAK..........ELEM SCHOOL.........JAN 24
    I-PAK..........ELEM SCHOOL.........JAN 23
    AIR HND......JOE'S FACTORY.......JAN 22
    AIR HND......JOE'S FACTORY.......JAN 21
    AIR HND......WESTSIDE MALL..... JAN 20

    Here's how I need for it to look:

    CHILLER.....WESTSIDE MALL.....JAN 28
    AIR HND.....WESTSIDE MALL.....JAN 27
    AIR HND.....WESTSIDE MALL.....JAN 25
    AIR HND.....WESTSIDE MALL.....JAN 20

    CHILLER.....ELEM SCHOOL........JAN 26
    I-PAK.........ELEM SCHOOL........JAN 24
    I-PAK.........ELEM SCHOOL........JAN 23

    AIR HND.....JOE'S FACTORY......JAN 22
    AIR HND.....JOE'S FACTORY......JAN 21

    Any suggestions? Thanks!
    Last edited by MineThatBird; 09-08-2013 at 01:28 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: Need macro that will group rows with duplicate data in a particular column together

    An example workbook would be a good idea. It looks like something you could do using Sort

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Need macro that will group rows with duplicate data in a particular column together

    Hi and welcome to the forum

    You dont need a macro for this, you can use excel's built-in sort function.

    Highlight the data, select Data Tab, select SORT
    1st sort would be Job Name
    2nd sort would be date
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Need macro that will group rows with duplicate data in a particular column together

    Excel 2007 => Data => sort

    Select all data

    sort on column B (job name)

    second condition sort on column C (date) from newest to oldest

    third condition sort on column A (name)
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  5. #5
    Registered User
    Join Date
    09-08-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need macro that will group rows with duplicate data in a particular column together

    Thank you Philb1, example workbook is attached.

    As you can see by looking at the "Incorrect Order" tab, using sort functions does not achieve the desired result (which you do see on the "Correct Order" tab, whose list I had to manually put in order).

    Workbook Example - Sort by ship date.xlsx

  6. #6
    Registered User
    Join Date
    09-08-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need macro that will group rows with duplicate data in a particular column together

    Thank you, FDibbins and oeldere. Unfortunately, using the sort functions does not achieve the desired result.

    It DOES bring equipment rows belonging to the same job together, and it DOES sort the equipment based upon ship date within that group itself. However, it DOES NOT list the aggregate jobs in proper order.

    This seems like the sort of thing that could be done with a macro... the program would need to scan the file for duplicate "Job Names", cut out those rows and paste them under the first instance of them in the list until it could not find any more duplicates. That would in fact achieve the desired result. Does anyone know how to write that sort of a macro?

  7. #7
    Registered User
    Join Date
    09-08-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need macro that will group rows with duplicate data in a particular column together

    Here is some VBA code that someone named sosasola posted in another forum... it seems like the right code, but for some reason it doesn't work properly when I apply it to my spreadsheet.

    The macro assumes that the 3 columns of data are placed in column A (Job Name), B (Product Type), C (Ship Date), the first task is to sort values in those columns the way that you suggested
    first: according the column A
    second: according the column C (descending)
    third: according the column B

    Next, it adds an extra temporary column and fills it up with temporary data, which is used for proper sorting operation. At the end of the code the temporary column is removed. It assumes that you have a header with Job Name, Product Type, and Ship Date, so the data starts from second row. Also, if data has more than 1000 rows, change all 1000's into data range or a bigger value.

    Does this look/sound right to you? Any thoughts on why it didn't work properly?

    Please Login or Register  to view this content.
    Please advise. Thanks!

+ 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. Macro to Group Rows Along a Range Based on Values in a Column
    By JHCali in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-06-2013, 10:20 AM
  2. [SOLVED] Need a Macro that will duplicate rows a number of times based on a column value
    By Lassitude in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-11-2013, 12:04 PM
  3. Copy column data by group/category, where the number of rows change
    By davidmurphy25 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2012, 10:25 PM
  4. Replies: 5
    Last Post: 06-11-2009, 08:57 AM
  5. Replies: 7
    Last Post: 06-19-2006, 01:20 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