Consulting

Results 1 to 8 of 8

Thread: VBA - Concatenate columns with delimiter

  1. #1

    VBA - Concatenate columns with delimiter

    Hello everybody!

    I have a question for a VBA code, and as a complete beginner in the world of VBA, I have no idea where to start.

    My goal is to concatenate some columns, the number will vary, with the delimiter " > " - no quotes, in the first cell of the row.
    I will be doing this for an unknown number of rows down, often several hundreds or thousands.

    Below are two pictures of the idea, the first showing the concept. I have a set of columns, and these need to be combined in the first cell of each row, in this case the empty A-rows.

    1.JPG

    The result should be like this, with all columns combined, separated by the delimiter >.
    2.JPG

    Hope somebody can help
    Last edited by Sinsation; 06-23-2022 at 02:59 AM. Reason: Solved

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,196
    Location
    Welcome to the forum, try the attached.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Fantastic! Thank you so much

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        Cells(1).Resize(3000) = [B1:B3000 & if(c1:c3000="","",">" & c1:c3000) & if(d1:d3000="","",">" & d1:d3000) & if(e1:e3000="","",">" & e1:e3000) & if(f1:f3000="","",">" & f1:f3000) & if(g1:g3000="","",">" & g1:g3000)]
    End Sub

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,196
    Location
    Quote Originally Posted by snb View Post
    Sub M_snb()
        Cells(1).Resize(3000) = [B1:B3000 & if(c1:c3000="","",">" & c1:c3000) & if(d1:d3000="","",">" & d1:d3000) & if(e1:e3000="","",">" & e1:e3000) & if(f1:f3000="","",">" & f1:f3000) & if(g1:g3000="","",">" & g1:g3000)]
    End Sub
    Why the 3000?

    Why not make the code dynamic as the TS has stipulated they do not know the row/ column count.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    you can also create a User-Defined function and use it in your worksheet.
    Attached Files Attached Files

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or a simple Excel formula:

    PHP Code:
    =SUBSTITUTE(TRIM(B1 &" " &C1 " " D1" " E1" " &F1" " &G1);" ";">"

  8. #8
    Another simply formula if excel has TEXTJOIN

    =TEXTJOIN(">",1,B1:M1) in A1 and drag down...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •