Consulting

Results 1 to 5 of 5

Thread: Concatanation in a new column

  1. #1
    VBAX Newbie
    Joined
    May 2017
    Posts
    3
    Location

    Concatanation in a new column

    Hi,

    I'm trying to concatenate cells of column A with cells of column C in the third column D. I wrote the code below but I can see only the last result in the column F.
    I think there is a problem with the incrementation ...
    Sub test()
    
    Sheets("feuil1").Activate
    
    Dim maplage1, maplage3, maplage4 As Range
    Dim c, d As Range
    Dim derligne1, derligne3 As Long
    
    derligne1 = Range("A" & Rows.Count).End(xlUp).Row
    derligne3 = Range("C" & Rows.Count).End(xlUp).Row
    
    Set maplage1 = Range(Cells(2, 1), Cells(derligne1, 1))
    Set maplage3 = Range(Cells(2, 3), Cells(derligne3, 3))
    Set maplage4 = Range("D2")
    
    For Each c In maplage1
        For Each d In maplage3
        
        maplage4 = c.Value & "_" & d.Value
        
        Next d
        Next c
        
    End Sub
    Does somebody can help!?!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You're overcomplicating it
    For Each c In maplage1
            c.Offset(, 3) = c.Value & "_" & c.Offset(, 2).Value
        Next c
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Newbie
    Joined
    May 2017
    Posts
    3
    Location
    Thank you for your answer!!

    It's not exactely what i'm trying to do. In fact I want to concatenate each cell of column A with each cell of column C. So if there are 2 values in A and 5 values in C I should have 10 concatenantions in column D.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    No problem
    For Each c In maplage1
            For Each d In maplage3
                i = i + 1
                Cells(i, "D") = c & "_" & d
            Next d
        Next c
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    May 2017
    Posts
    3
    Location
    Thank you very much! It's ok!

Posting Permissions

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