Consulting

Results 1 to 12 of 12

Thread: Macro to combine columns without formulas

  1. #1

    Macro to combine columns without formulas

    Good Morning all,

    I need a macro to combine any columns I select into one column, so if I chose columns A,B,C,D then all data from these columns will be merged into column A with a space, or if I selected Columns D to T then all data from columns D to T would be merged into column D with a space,

    Each column must keep the original data on the same row and the columns originally selected are deleted after merging.
    Each Column will have varied data with different number of rows
    Headers are to be included with this merge
    I am using Excel 2007
    (the dots are only used to break up the words into columns)
    Example

    ......A........ B........ C........ D......... E
    1 Hi.........How......Are......You......Hello
    2 ........... How
    3 ....................... Are
    4 .................................. You

    Merged to this

    ...............A....................B..........C.......D.......E....
    1 Hi How Are You..........Hello
    2 How
    3 Are
    4 You

    Any help is appreciated,

    Thanks in advance

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You'd better use a macro.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Maybe something like this

    I made it so you don't require a contiguous selected range

    In the attachment you can select all of A : D to combine into A, as well as just M and O (hold the control key down and select M and then O) to combine into M


    Option Explicit
    Sub CombineColumns()
        Dim rUsed As Range, rArea As Range, rCell As Range, rFirst As Range, rColumn As Range
        
        If Not TypeOf Selection Is Range Then Exit Sub
    
        Set rUsed = Intersect(Selection.EntireColumn, ActiveSheet.UsedRange)
    
        If rUsed Is Nothing Then Exit Sub
        
        Application.ScreenUpdating = False
        
        Set rFirst = rUsed.Columns(1)
    
        For Each rArea In rUsed.Areas
            For Each rColumn In rArea.Columns
                If rColumn.Address <> rFirst.Address Then
                    For Each rCell In rColumn.Cells
                        If Len(Trim(rCell.Value)) > 0 Then
                            rFirst.Cells(rCell.Row, 1) = rFirst.Cells(rCell.Row, 1) & " " & Trim(rCell.Value)
                        End If
                    Next
                    
                    rColumn.ClearContents
                End If
            Next
        Next
        Application.ScreenUpdating = False
    
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 02-01-2016 at 02:49 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    My apologies for not referencing a cross post, but I wasn't sure if anyone would answer here and I got a reply on another forum

  7. #7
    I have tried the code posted above but it errors on the word Trim - If Len(Trim(rCell.Value)) > 0 Then
    Error is Compile Error: Wrong number of arguments or invalid property assignment

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Complies OK here

    As a possibility, go to Tools, References and check


    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Compiles OK here

    As a possibility, go to Tools, References and check


    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Hi Paul I don't have version 16.0, I only have version 12.0

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Version number doesn't matter
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Is OK -- just go by the title, but Len and Trim are part of VBA ..... unless you've defined a function with the same name and then Excel might get confused
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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