+ Reply to Thread
Results 1 to 6 of 6

Macro that can merge a column of cells into one cell separated by semi colon

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-30-2013
    Location
    arizona
    MS-Off Ver
    365
    Posts
    147

    Macro that can merge a column of cells into one cell separated by semi colon

    Hello,

    I need a macro that can take all the values of all the cells in a single column (I) and combine them into one cell (J11) separated by a semicolon.
    I also need to macro to work across all worksheets except for "Sheet1", "Sheet2" and "Sheet3".

    I found the following code which works well but it does not seperate by semi colon or work across all sheets.

     
    Dim v As Variant
    Dim s As String
    Dim i As Long
    
    ' Fetch from sheet
    v = Range("A1:A100").Value
    
    ' Concatenate into a single string
    s = ""
    For i = LBound(v, 1) To UBound(v, 1)
        s = s & v(i, 1)
    Next i
    
    ' Put back on sheet
    Range("B1").Value = s

    Can anyone help me with this?
    Thanks

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that can merge a column of cells into one cell separated by semi colon

    Hi Glide!

    Try This..
    Dim v As Variant
    Dim s As String
    Dim i As Long
    For Each sh In ActiveWorkbook
     Select Case sh.Name
       Case "Sheet1", "Sheet2" And "Sheet3"
       Case Else
         With sh
         ' Fetch from sheet
         v = .Range("A1:A100").Value
         
         ' Concatenate into a single string
         s = ""
         For i = LBound(v, 1) To UBound(v, 1)
             s = s & ";" & v(i, 1)
         Next i
         
         ' Put back on sheet
         .Range("B1").Value = Mid(s, 2)
        End With
     End Select
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro that can merge a column of cells into one cell separated by semi colon

    v = .Range("A1:A100").Value
    Column I

  4. #4
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that can merge a column of cells into one cell separated by semi colon

    @ AB33!

    Thanks for the catch..

    Dim v As Variant
    Dim s As String
    Dim i As Long
    For Each sh In ActiveWorkbook
     Select Case sh.Name
       Case "Sheet1", "Sheet2" And "Sheet3"
       Case Else
         With sh
         ' Fetch from sheet
         v = .Range("I1:I" & Cells(Rows.Count, "I").End(-4162)).Value
         
         ' Concatenate into a single string
         s = ""
         For i = LBound(v, 1) To UBound(v, 1)
             s = s & ";" & v(i, 1)
         Next i
         
         ' Put back on sheet
         .Range("B1").Value = Mid(s, 2)
        End With
     End Select

  5. #5
    Forum Contributor
    Join Date
    04-30-2013
    Location
    arizona
    MS-Off Ver
    365
    Posts
    147

    Re: Macro that can merge a column of cells into one cell separated by semi colon

    Thank you that works great! However, I did notice that it places a semi colon for each row regardless of whether there was a value in that cell. Do you know how to modify this code so that it only copies values if there is a value in the cell? I am trying to avoid a long list of semicolons. Thanks a ton.

  6. #6
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro that can merge a column of cells into one cell separated by semi colon

    Hi Glide

    Try This..

    Dim v As Variant
    Dim s As String
    Dim i As Long
    For Each sh In ActiveWorkbook.Worksheets
     Select Case sh.Name
       Case "Sheet1", "Sheet2", "Sheet3"
       Case Else
         With sh
         ' Fetch from sheet
         v = .Range("I1:I" & .Cells(.Rows.Count, "I").End(-4162).Row)
         
         ' Concatenate into a single string
         s = ""
         For Each cl In .Range("I1:I" & .Cells(.Rows.Count, "I").End(-4162).Row).SpecialCells(2)
            If Not cl Is Nothing Then _
             s = s & ";" & cl
         Next cl
         
         ' Put back on sheet
         .Range("B1").Value = Mid(s, 2)
        End With
     End Select
    Next sh

+ 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