+ Reply to Thread
Results 1 to 26 of 26

Merge columns based on condition

  1. #1
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Merge columns based on condition

    I am new to Macro coding so this should be a simple request but the code of these can be intimidating and cumbersome.

    Basically I want to merge the yellow rows into one column when the ID = 2 but keep the table of values untouched above it after macro is run for each instance of ID =2.
    Attached Images Attached Images

  2. #2
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    this could easily be done with a macro but what are you trying to merge exactly?
    Edit: do you mean if ID = 2, copy & paste the entire row to a new column?

  3. #3
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    No, I just want to merge the yellow fields in to one field and keep the values in the table above it in same format. This way the very long sentence will show under the column.

    Of course my follow up question once this is done will be how do I wrap the text also.

    So it looks like this: example.png

  4. #4
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Follow-up question.... does the subsequent text get longer, or do you want all "Stuff" text to be equal? when you say MERGE I'm thinking we're merging two or more cells into one but I don't see that from the photo shared.

  5. #5
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    The "Very long sentence" varies from "N/A" to a very long string and everything in between. I deleted the original string and typed that in instead as dummy data just for the example.

  6. #6
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Notice the two images and see how I want to merge the data into one cell but keep table of numeric values in same format.

  7. #7
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    You never want to "Merge and Center" when working with VBA... you can, however, format the cell height and width and wrap the text within that cell. The only problem with that is the column width will change for all cells in the column

  8. #8
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Quote Originally Posted by NerdyDataGuy View Post
    Notice the two images and see how I want to merge the data into one cell but keep table of numeric values in same format.
    Yes, I see that & I just responded with Merge and Center is not VBA friendly.

  9. #9
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    I initially thought you wanted to merge two or more strings, which is easy enough to do in VBA.

  10. #10
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Oh, I did the merge and center by accident to the numeric values. Disregard that part.

    I would like to merge the very long sentence row from "Stuff" column to be Stuff + a+b+c+d+e+f+g columns

    so wherever ID = 2 merge those cells and leave the numbers alone.

  11. #11
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Yes, I can do that, no problem!

  12. #12
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Edited @ 3:40 pm est to include the stuff column in the final concatenation.
    Please Login or Register  to view this content.
    Last edited by carlmon; 05-24-2022 at 03:40 PM.

  13. #13
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    if you had more than 8 columns, I would have looped through the columns, but because its so few I wrote a code that gives a better visual representation of what's happening.

  14. #14
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    I was trying to figure out why it didnt work and then it made it worse with each attempt.

    But when I simply plug in your code to same spreadsheet I get the following image. The value I wanted to merge became 0 and didnt merge the cells.Attachment 781574

  15. #15
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    try sharing the picture again - I recreated your spreadsheet and it worked for me.... are you running the code on the same sheet?


    & take a minute to look at the yellow ribbon at the top, we could work this out much faster with a shared workbook

  16. #16
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Attachment 781577 oops see attached.

  17. #17
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    again, not attached.

    Attach via "Go Advanced" at the bottom near the post button & attach from there. For whatever reason, attachments don't work through the basic response window.

  18. #18
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Think this worked.
    Attached Files Attached Files

  19. #19
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    It did work...Thanks. give this a try in the workbook that you shared.
    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Didnt merge the rows as I was hoping.
    Attached Files Attached Files

  21. #21
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Quote Originally Posted by NerdyDataGuy View Post
    Didnt merge the rows as I was hoping.
    Like I said earlier, you really don't want to merge cells in VBA... BUT if you really wanted to do it that way, this code (below) will work:

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    This worked, but how would this work if I moved ID column to the end of this table and included another row at beginning?

    See attached.

  23. #23
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    I don't see the attached item... but to do what you're describing, assuming you meant adding a column and not a row in place of the ID column, you would have to change your references as it relates to the ID column...

    for example, instead of offsetting (0,2) for a, you would offset (0,-8). Without seeing the workbook, it would be difficult to change the code accordingly.

  24. #24
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    Think I forgot to hit upload for the file.
    Attached Files Attached Files

  25. #25
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Merge columns based on condition

    Here you go:

    Please add rep w/ the bottom star if I helped you out
    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    05-10-2022
    Location
    Denver Colorado
    MS-Off Ver
    Windows 10
    Posts
    14

    Re: Merge columns based on condition

    I was playing with this and trying the get it to work changing some of the numeric values.

    Got close but didnt get there.

    This code merges all the columns except last into one cell and removes the rest of the data too.
    Attached Files Attached Files

+ 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. split two columns to multiple columns based on condition
    By Alaa-A in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-15-2021, 11:20 PM
  2. [SOLVED] Merge multiple columns into a single column with condition
    By arindamsenaxa in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-17-2017, 10:07 AM
  3. Merge values in a single cell based on condition
    By bajrang2101 in forum Excel General
    Replies: 3
    Last Post: 12-09-2014, 06:45 AM
  4. Merge based on columns if there are duplicated data
    By danitzia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2014, 06:18 AM
  5. Merge All Sheets Data Into Master Sheet Based on a condition
    By alfykunable in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-29-2012, 05:24 AM
  6. Merge Two Columns Based on Header
    By MSmithson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2010, 05:29 PM
  7. Merge ranges based on condition
    By BrianDP1977 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2005, 09:20 PM

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