+ Reply to Thread
Results 1 to 5 of 5

Nested loop to concatenate 2 columns of data

Hybrid View

JohnnyBGood Nested loop to concatenate 2... 10-27-2009, 03:07 PM
Leith Ross Re: Nested loop to... 10-27-2009, 05:00 PM
mikerickson Re: Nested loop to... 10-27-2009, 05:18 PM
JohnnyBGood Re: Nested loop to... 10-27-2009, 09:11 PM
pvr1983 Re: Nested loop to... 07-15-2015, 07:42 AM
  1. #1
    Registered User
    Join Date
    12-20-2005
    Posts
    44

    Nested loop to concatenate 2 columns of data

    Hi,

    I think I just need a basic Nested For Loop code...

    I have a list of in column A, from A1:A537

    and another list in column B, for B1:B50

    I want to add a column that concatenates,

    each item in Column A to B1 (so 537 rows), then again each item in Column A to B2, another 537 rows, etc.. this loop is repeated 50 times, one for each name in column B.
    Last edited by JohnnyBGood; 10-27-2009 at 09:12 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Nested loop to concatenate 2 columns of data

    Hello JohnnyBGood,

    Here is a macro you can use. It uses column "C" as the helper column.
    Sub ConcatenationLoop()
    
      Dim RngA As Range
      Dim RngB As Range
      Dim RngC As Range
      Dim I As Long
      Dim J As Long
      Dim N As Long
      
        Set RngA = Range("A1:A537")
        Set RngB = Range("B1:B50")
        Set RngC = Range("C1")
        
          For I = 1 To RngB.Rows.Count
            For J = 1 To RngA.Rows.Count
              N = N + 1
              RngC.Cells(N, 1) = RngA.Cells(J, 1) & RngB.Cells(I, 1)
            Next J
          Next I
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Nested loop to concatenate 2 columns of data

    There is no need for a loop
    Sub test()
        Dim lastA As Long
        With ThisWorkbook.Sheets("sheet1")
            lastA = .Cells(.Rows.Count, 1).End(xlUp).Row
            With Cells(1, 5).Resize(lastA * .Cells(.Rows.Count, 2).End(xlUp).Row, 1)
                .FormulaR1C1 = "=OFFSET(R1C1,MOD(ROW(RC1)-1," & lastA & "),0)" & "&OFFSET(R1C2,INT((ROW(RC1)-1)/" & lastA & "),0)"
                .Value = .Value
            End With
        End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    12-20-2005
    Posts
    44

    Re: Nested loop to concatenate 2 columns of data

    Thanks very much guys...

    Leith, I was close to yours, except that N=N+1 part....

    I couldn't figure out how to go past row 537 to keep adding fields.

  5. #5
    Registered User
    Join Date
    12-30-2011
    Location
    delhi
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Nested loop to concatenate 2 columns of data

    dear friends i was searching for concatenating column with row data
    i found this thread i have a different proposition
    i have values in coloumn a and row 2 (these are all in sheet1)
    i need these values to be concatenated in the following manner in sheet 2
    sheet2A2=sheet1(a2,b2)
    sheet2A3=sheet1(a2,c2)
    sheet2A4=sheet1(a2,d2)
    sheet2A5=sheet1(a2,e2)

    until it finds the empty cell in row 2
    then it has to loop for A3 then A4 etc

    regards
    pvr

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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