Consulting

Results 1 to 8 of 8

Thread: combine values from multiple columns into a single cell in column

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    combine values from multiple columns into a single cell in column

    I need a VBA code that can combine values from multiple columns into a single column that mean if i have a data on column A2-A3-A4-A30-AA-AB... combine all of them into A1 i have this VBA code but it is not work on my data, because the column A is empty . i attached a sample file enclosed this post.

    Thank you for your help


    Sub Comb() 
    Dim i As Long 
    For i = 1 To Range("A" & Rows.count).end(xlup).row Step 1 
        For x = 2 To 256 
        If Cells(i, x).Value = "" Then 
        Else 
        Cells(i, 1).Value = Cells(i, 1).Value & ", " & Cells(i, x).Value 
        End If 
        Next x 
    Next i 
    End Sub
    Attached Files Attached Files
    Last edited by parscon; 11-28-2013 at 12:17 PM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    I'm not 100% sure what you mean.
    Try changing:
    'For i = 1 To Range("A" & Rows.Count).End(xlUp).Row Step 1
    to:
    For i = 1 To ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    just u I need all data on row A combine to A1 and above code will do it but it if column A is empty it will not work.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    It worked here when I tried it on your data after making the change suggested in messgae#2.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by parscon View Post
    just u I need all data on row A combine to A1 and above code will do it but it if column A is empty it will not work.
    actually it works. but if Column A is blank that means Range("A" & Rows.Count).End(xlUp).Row is 1.

    so the For Next Loops only once.

    your worksheet contains data from Column B to Column AA, from Row 1 to Row 2718. clearly define what you really want. if you want to concatenate cell values from col B to col AA in col A in the same row, follow p45cal's solution.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    there are blank rows in your table.

    Sub Combine_Cells_ColB_thru_ColAA_in_ColumnA()
    
    
        Dim LastRow As Long, LastCol As Long
        Dim i As Long, j As Long, Calc As Long
        
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            Calc = .Calculation
            .Calculation = xlCalculationManual
        End With
        
        With ActiveSheet
            .Columns("A").Clear
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
            For i = 1 To LastRow
                For j = 2 To LastCol
                    If .Cells(i, j).Value <> "" Then
                    .Cells(i, 1).Value = Mid(.Cells(i, 1).Value & ", " & .Cells(i, j).Value, 3)
                    End If
                Next j
            Next i
        End With
    
    
        With Application
            .Calculation = Calc
        End With
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you very much for your help

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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