+ Reply to Thread
Results 1 to 12 of 12

Macro to combine two rows with the same id

  1. #1
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Talking Macro to combine two rows with the same id

    Hello all,
    Here is a sample of a large data set:
    Capture.PNG

    I want to combine two rows with the same id into one row. There are three types Primary, No record and Spouse. When combining, the primary and no record should always be at the left. Also, different people have different numbers of phone#, like John has four and Becky has seven and others might have 10. All the combined spouse should be placed after the longest row, like in the sample after seven. Here is the result:
    Capture1.PNG
    Capture2.PNG

    I'm wondering if that is possible. Thank you for your help
    Attached Files Attached Files
    Last edited by Luden; 09-18-2019 at 04:19 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    Please Login or Register  to view this content.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Re: Macro to combine two rows with the same id

    Quote Originally Posted by alansidman View Post
    Please Login or Register  to view this content.
    Hello Alan,
    I tried your code, but still when the primary is one row under the spouse, the primary goes behind.
    Also, I have people who have like 17 phone numbers. The code overwrites the data behind phone number 7. Thanks
    e1.PNG
    e2.PNG

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    How about this adaption?
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Re: Macro to combine two rows with the same id

    Quote Originally Posted by alansidman View Post
    How about this adaption?
    Please Login or Register  to view this content.

    Thank you, Allen. The primary part is ok and the combining doesn't overwrite the data. Now is it possible to move all the spouses to cells right after the RIGHTMOST column? Say I have a person who have 17 phone numbers, meaning the rightmost column is called Phone Number 17. Then all the data starting from spouse go after the column Phone Number 17. The words spouse are all in one column and don't overwrite the data.
    I'm trying to be specific, but maybe too complicated lol

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    Because we have now appended the spouse data to the next available column for each row, I really have no way of knowing where the longest row for the Primary is located. Let me think about a workaround to determine the widest record before cutting and pasting. Getting late here and will look at this later when I have time. It is now sleepy time.

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    Had a brain spurt before getting off the couch.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Re: Macro to combine two rows with the same id

    Quote Originally Posted by alansidman View Post
    Had a brain spurt before getting off the couch.

    Please Login or Register  to view this content.
    Wow, works like a charm. Thank you so much, Allen! BTW, should include lcc As Long

  9. #9
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Re: Macro to combine two rows with the same id

    Quote Originally Posted by alansidman View Post
    Had a brain spurt before getting off the couch.

    Please Login or Register  to view this content.

    Sorry Allen,
    When I carefully test your code, I found that when combining primary and spouse, even though the spouse has like 6 phone numbers, it only shows four numbers.
    All the spouses have three or four phone numbers no matter how many phone numbers they originally have.
    If Mary has 7 numbers:
    Capture1.PNG
    After combining, there are only five numbers:
    Capture2.PNG

    Is that because you didn't define lcc? When I executed the code, I put lcc As Long. Maybe that's the problem? Thank you!

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    Forgot to define lcc. Good on you for finding that. Long is correct. Once you did that, did the code run appropriately or was there still an issue.

  11. #11
    Registered User
    Join Date
    09-13-2019
    Location
    US
    MS-Off Ver
    office 365
    Posts
    26

    Re: Macro to combine two rows with the same id

    Quote Originally Posted by alansidman View Post
    Forgot to define lcc. Good on you for finding that. Long is correct. Once you did that, did the code run appropriately or was there still an issue.
    Hello Allen,

    Sorry for the late reply. I was able to troubleshoot the problem. Thank you anyway.

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,870

    Re: Macro to combine two rows with the same id

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. 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 combine multiple rows in to one row
    By robbiejw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2013, 03:07 AM
  2. Macro to Combine Rows of Data
    By jsolder in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-26-2013, 12:58 PM
  3. [SOLVED] Help! Macro: Combine rows with 'same name entry'
    By cjhiggins in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-26-2013, 11:23 AM
  4. [SOLVED] Help with macro to combine rows
    By theatergirl in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-09-2013, 05:02 PM
  5. [SOLVED] Macro to combine rows
    By theatergirl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2013, 10:16 AM
  6. [SOLVED] Help with a macro to combine rows in a spreadsheet
    By theatergirl in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-21-2013, 10:17 AM
  7. Macro to combine Rows
    By floridagunner in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2008, 07:29 PM
  8. macro to combine rows and add numbers
    By madi101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2008, 11:37 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