+ Reply to Thread
Results 1 to 10 of 10

Sorting and grouping data according to one and two words of a Column

  1. #1
    Forum Contributor
    Join Date
    04-04-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    204

    Sorting and grouping data according to one and two words of a Column

    Hi Guys,

    I have worksheet1 that has data of 8 columns (A-H) and nearly 7000 rows. All data in are sorted according Column D which is also sorted alphabetically.

    What I am after is a VBA script that will additionally sort data in Worksheet1 according to Column D, but in this time according to the first and second words of each cell in Column D.

    As per uploaded example, all items in Column D that the same first word are moved and sorted in Worksheet2 with an empty row separating each group of data according to that first word.

    The same is for Worksheet3 where all items in Column D of Worksheet1 that have the same first two words are moved and sorted with an empty row separating each group of data according to both first and second words.

    Items in Column D of Worksheet1 with different first, and first and second words are just ignored to stay in Worksheet1.

    Can someone generously help with this matter?

    Thanks in advance.list_of_subjects_example.xlsx
    Last edited by terryhenderson; 08-03-2015 at 07:16 PM.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Sorting and grouping data according to one and two words of a Column

    Here you go:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Contributor
    Join Date
    04-04-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    204

    Re: Sorting and grouping data according to one and two words of a Column

    Thank you very much daffodil11 for your invaluable input with my question. In fact, as I ran the first script it paused with run-time error '5' message "invalid procedure call or argument". Then running the second script, it did as exactly as needed like magic. Is it possible that the script will move those rows from Worksheet1 to other worksheets instead of just copying?
    My deep thanking to you for all your effort in this regard.

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Sorting and grouping data according to one and two words of a Column

    Perhaps I misunderstood some of the logic. Are all the 2-word matches going to sheet 3, with everything else going to sheet2?

  5. #5
    Forum Contributor
    Join Date
    04-04-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    204

    Re: Sorting and grouping data according to one and two words of a Column

    Thanks for replying. Actually, the final product after running the script will be three portions of data divided onto three working sheets: the one-word portion in WS2, the two-word portion in WS3 and non-matching or remaining odds will stay in the first worksheet fur further manipulation. I hope this will make it much clearer. Thanks very much for your cooperation.

  6. #6
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Sorting and grouping data according to one and two words of a Column

    Cool, I'll stay late and give it a shot.

    Edit: Okay, that failed. Since we need to kill the data, the detection to determine if it's recurring breaks for the last occurrence. Maybe I'll try a scripting dictionary to save the row numbers.

    example: if we cut Advanced Computer 1, 2, and 3, when it gets to the 4th one it doesn't see the multiplicity and so scraps it. HRMMMMMM!
    Last edited by daffodil11; 08-04-2015 at 06:19 PM.

  7. #7
    Forum Contributor
    Join Date
    04-04-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    204

    Re: Sorting and grouping data according to one and two words of a Column

    Yes, you're right. In this case I may suggest that we would have two versions of the first script (one word): one for copying and the other for deleting. First version to run first, where the product is WS2. Then the two-word script is run to move the selected rows to WS3. The last step is to run the second version of the first script to delete the records that were copied to WS2. Does this make sense? If not, please pardon my logic, I'm just trying to help : )

  8. #8
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Sorting and grouping data according to one and two words of a Column

    Give this a shot, took a laughable 3 hours to write because my VBA-fu is terrible.

    It pulls the 2 word matches first to sheet three, and then consolidates the source. Then it pulls single word matches, and consolidates a 2nd time:

    Please Login or Register  to view this content.
    I shall endeavor to learn code arrays in the future; but here's the general logic:

    It runs through and detects if the first two words of a cell exist as any other first two words. If so, it writes down the row number into a dictionary (imaginary spreadsheet) along with those two words.
    When it completes, it then goes back through the dictionary and compares each set of words to the previous set. (The first set has no previous, but I coded around that). For a matching set it copies from the memorized row number, to the next line on the respective sheet. For an unmatched set it copies from the row number and skips a row at the paste-to location. Then it deletes that row and moves on.

    It repeats in a similar fashion for the single word matches.
    Attached Files Attached Files
    Last edited by daffodil11; 08-04-2015 at 08:25 PM. Reason: removed my silly debug.prints and stops

  9. #9
    Forum Contributor
    Join Date
    04-04-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    204

    Re: Sorting and grouping data according to one and two words of a Column

    Thank you very much for such genius code, with its genius logic. I wouldn't ever produce something like this even in ten years.
    It ran smoothly and did as exactly as needed. It just paused on records with one word only, which I had to extract on another worksheet, rather than that everything went as desired. Now the product of running the code is ready for another manipulation thanks to your efforts.
    You do have a very valuable input to this world, please keep it up as this world does need you : )
    ALL THE BEST..

  10. #10
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Sorting and grouping data according to one and two words of a Column

    Thanks for your feedback.

    It definitely pushed some of my skills to the limit and I learned a few new bits along the way, such as that the .Keys of a Dictionary is a collection.
    After I studied up on some of its properties I was able to franken-code the rest of it together.

+ 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] Sorting Data and Grouping
    By Forex-Forex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2014, 05:19 PM
  2. Sorting first and second words in one column into two other columns....
    By esy0008 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-07-2012, 02:08 PM
  3. Need help grouping by similar words in second column
    By bigmark1972 in forum Excel General
    Replies: 11
    Last Post: 11-19-2011, 09:09 PM
  4. Grouping and Sorting Data into Sub sections
    By Impartial Derivative in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2011, 12:20 PM
  5. Auto-Sorting and Grouping data by column
    By CJMathias in forum Excel General
    Replies: 4
    Last Post: 10-24-2010, 12:14 PM
  6. Data Sorting/Grouping Function
    By Blake 7 in forum Excel General
    Replies: 0
    Last Post: 10-19-2010, 02:50 PM
  7. Grouping and sorting data
    By coffee_n_tv in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-29-2008, 06:44 PM
  8. grouping and sorting data
    By mb7q in forum Excel General
    Replies: 0
    Last Post: 03-30-2006, 03:35 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