+ Reply to Thread
Results 1 to 5 of 5

Macro to combine data from two datasets based on matching strings

  1. #1
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Macro to combine data from two datasets based on matching strings

    Hello everyone,

    In summary:
    I'd really like your help creating VBA code that looks through two datasets, finds matching strings that start "Title:", and then copies associated rows from one dataset to the other.

    This workbook shows the 'before' and desired 'after':
    sorting-music-data.xlsx

    In detail:
    I deal with spreadsheets listing information about music tracks. They're not arranged in a very conventional way - but that's out of my control!

    Different pieces of information about the same song appear in two separate sections on the same worksheet.

    This info needs to be brought together. This takes a long time to do manually, so I am looking at a VBA solution.

    The two sections are called 'Alphabetical Order' and 'In The Order Played'.

    Ultimately I need all the tracks to be listed in the same order as in the 'In The Order Played' section.

    Each track has at the very least a Title: field; this is present in both sections. This could be used to help match the data in the two sections up.

    I attach a sample worksheet showing the 'before' and 'desired' states.

    sorting-music-data.xlsx

    Notes about the two sections:
    • In the 'Alphabetical Order' section, the only mandatory field is Title. The number of additional fields, and their names, varies.
    • In the 'In The Order Played' section, three fields are always given about each track: Title, Duration and Offset.

    Other Notes:
    • In reality there'd be perhaps 10, maybe even 50 separate tracks rather than five.
    • The solution code would need to deal with whatever track names were thrown at it.
    • Sometimes the same song may appear multiple times in the 'In The Order Played' section, so the same data will need to be fetched from the 'Alphabetical Order' section. It's not a good idea, therefore, to cut or delete anything from this 'alphabetical' section until the end.
    • I've used colour on the worksheet to help illustrate the 'before' and 'after' states - but there wouldn't be colour 'in real life'.
    • The output needs to be formatted as indicated, so that a computer program at work can parse it. It doesn't mind what order the fields are in, as long as there is a blank line between each new track.


    Pseudo-code:
    Here's some pseudo-code setting out one way I thought the task might be achieved, but lack the programming knowledge to implement...

    For the range between the string "In The Order Played" and the last used cell on the worksheet

    For each cell starting with "Title: "

    Look at the full text string in this cell and find a matching string in the "Alphabetical Order" section.

    When found, in the "Alphabetical Order" section, copy all the rows below the relevant "Title" field, until (but not including) the next blank row

    Look at how many rows have just been copied and insert this many rows beneath the appropriate "Title" in the "In The Order Played" section

    Paste the rows

    Repeat the process by looking at the next cell starting with "Title: " in the "In The Order Played" section

    Keep going until the end of the used worksheet is reached

    After this loop is over: Delete all of the "Alphabetical Order" section, and the "Alphabetical Order" and "In The Order Played" section titles.


    ***

    I'd really appreciate your help - I'm off to bed now, so apologise if I don't get back to you with a reply right away. I'll be back online in a few hours!

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro to combine data from two datasets based on matching strings

    Hi mfd, first post after lurking for 4 years? Welcome to the bright side

    Try the attached file. Click on the button, a new sheet will be created.
    Attached Files Attached Files
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Macro to combine data from two datasets based on matching strings

    :-) Haha, I can be a bit reluctant to ask for help!

    Thanks so much millz - it works a dream. It will make my life a lot easier.

    There's one eventuality I hinted at in my opening post, but neglected to include in my sample data. This is where in the 'Alphabetical' section, a track has only a Title, and no other attributes.

    Currently when this happens, the code throws up a '1004' run-time error. The debugger highlights the following line:
    Please Login or Register  to view this content.
    You can see an example in this workbook:
    what-if-its-just-the-title.xlsm
    It's the absence of a value in the line beneath Title: 4th of July (Instrumental) that seems to be doing it.

    But once again, my grateful thanks for your help.

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro to combine data from two datasets based on matching strings

    Hi mfd, yes actually after I posted, I thought of the possibility of that error occurring, but I thought it could be unlikely there isn't any performer -- I was so wrong haha (you can also think of it as me just being lazy). Made some amendments to the code now, try again.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-06-2010
    Location
    Derby
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Macro to combine data from two datasets based on matching strings

    :-) Not lazy at all! Every track ought to have a performer... it's just that sometimes they're missing from my data.

    Anyway, your code is absolutely spot on. Thanks very much indeed for your help.

+ 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. Replies: 0
    Last Post: 10-11-2013, 05:24 AM
  2. [SOLVED] Macro to sort column datasets based on totals
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-10-2011, 06:20 PM
  3. Matching IDs from 3 datasets
    By tripvanwinkel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-25-2011, 12:47 PM
  4. Combine Rows Based on Matching Cells in A
    By LeviThomason in forum Excel General
    Replies: 1
    Last Post: 06-28-2008, 04:27 PM
  5. Macro to combine data based on IF,THENs?
    By marlea in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-15-2005, 05:32 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