+ Reply to Thread
Results 1 to 4 of 4

Thread: Concatenate Multiple Cells

  1. #1
    Registered User
    Join Date
    12-11-2008
    Location
    India
    Posts
    6

    Lightbulb Concatenate Multiple Cells

    Hi, i require a VBA script which can concatenate different cells on the same sheet, and add them to a single line, while deleting the rest. OR copying them to a new worksheet.

    Please see the attachment for furhter details. I have given clear example fo ryou to understand better.

    Thanks
    ROhit.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: Using VBA to Concatenate Multiple Cells - attchement

    Hi Rohit

    Try the attached - run the macro called Concatenate.

    Dion
    Attached Files Attached Files

  3. #3
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Using VBA to Concatenate Multiple Cells - attchement

    Another possible route would be something like:

    Public Sub Example()
    Dim lngSno As Long, lngMax As Long, lngRowS As Long, lngRowE As Long, vData As Variant
    lngMax = 1 + Application.Max(Columns(1))
    With Columns(4)
        .Insert
        .Offset(, -1).Cells(2).Value = "Address"
        .Offset(, -2).Cells(2).Value = "Name"
    End With
    Cells(Rows.Count, "C").End(xlUp).Offset(1, -2).Value = lngMax
    For lngSno = 1 To lngMax - 1 Step 1
        lngRowS = Application.Match(lngSno, Columns(1), 0)
        lngRowE = Cells(lngRowS, "A").End(xlDown).Row
        vData = Application.Transpose(Range(Cells(lngRowS + 1, "C"), Cells(lngRowE - 1, "C")))
        With Cells(lngRowS, "D")
            .Value = Join(vData, Chr(10))
            .WrapText = True
        End With
    Next lngSno
    On Error Resume Next
    Range(Cells(4, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(, 1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub
    (certain assumptions are made in line with content of sample - ie blanks rather than nulls)

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Using VBA to Concatenate Multiple Cells - attchement

    Hello rohit475,

    Here is another version of the macro. A button has been added to the "Current" worksheet to run the macro in the attached workbook. Here is the code.
    Sub ConcatenateRows()
    
      Dim Addx As String
      Dim N As Long
      Dim R As Long
      Dim Rng As Range
      Dim Wks As Worksheet
        
        Set Wks = Worksheets("Current")
        
          Wks.Columns("D").EntireColumn.Insert Shift:=xlToRight
          Set Rng = Wks.UsedRange
          Rng.Cells(1, "C") = "Name"
          Rng.Cells(1, "D") = "Address"
          
            N = 3
          
            For R = 3 To Rng.Rows.Count
              If Rng.Cells(R, "B") = "" Then
                 Addx = Addx & Rng.Cells(R, "C") & vbLf
              Else
                 If Len(Addx) > 1 Then Rng.Cells(N, "D") = Left(Addx, Len(Addx) - 1)
                 N = R
                 Addx = ""
              End If
            Next R
          
          If Len(Addx) > 1 Then Rng.Cells(N, "D") = Left(Addx, Len(Addx) - 1)
          
          On Error Resume Next
          Set Rng = Rng.Offset(2, 0).Resize(Rng.Rows.Count - 2, 1)
          Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        
    End Sub
    Attached Files Attached Files
    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!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0