Consulting

Results 1 to 6 of 6

Thread: VBA to merge data and place - between each data from five columns

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    VBA to merge data and place - between each data from five columns

    I have data in the columns D E F G H and want to be able to select this data and run a macro that would merge these columns so data would look like this
    A1-A6-B12-C26-D2
    B1-C13-D8-D10-E8
    A1-A6-B13-C27-D3
    B2-C14-D8-D10-E9
    A1-A6-B14-C28-D4

    see example in file.
    Thank you
    Attached Files Attached Files
    Last edited by estatefinds; 04-13-2016 at 07:12 AM.

  2. #2
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    Hi Dennis,

    Give this a go...

    Sub Mrg_Columns_D_To_H()
         '   ---------------------------------------------------------------------------------------
         '   Concatenate columns "D:H".
         '   ---------------------------------------------------------------------------------------
        With Application
            .ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
        End With
        Range("J1:J" & Range("D" & Rows.Count).End(xlUp).Row).Formula = _
            "=D1&""-""&E1&""-""&F1&""-""&G1&""-""&H1"
        Columns("J").EntireColumn.AutoFit
        With Application
            .DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
        End With
    End Sub
    I hope this helps!
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    simple, brute force

    Sub MergeCells()
        Dim iRow As Long
        
        For iRow = 1 To Range("D1").CurrentRegion.Rows.Count
            Cells(iRow, 10).Value = Cells(iRow, 4).Value & "-" & _
                    Cells(iRow, 5).Value & "-" & _
                    Cells(iRow, 6).Value & "-" & _
                    Cells(iRow, 7).Value & "-" & _
                    Cells(iRow, 8).Value
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    Hi Dennis,

    You might want to clear the contents of column "J" first before you run the program.
    You may also just want the values as opposed to the formulas to show in column "J".
    You can just adapted the code accordingly to do either or both of these.

    EDIT: I have just noticed that some of the cells in columns "D:H" have an extra space at the end of the values, this is giving a space for those values when they are output to column "J", you could also incorporate the TRIM function into the code as well.
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Great job!!! thanks again I was struggling with merges before.
    Thanks again it works great!!!
    I tried both and work perfectly!!!

  6. #6
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    Quote Originally Posted by estatefinds View Post
    Great job!!! thanks again I was struggling with merges before.
    Thanks again, it works great!!!
    I tried both and work perfectly!!!
    You're welcome, thanks for the feedback.

    Don't forget to include the extra lines of code so it excludes errors.
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

Posting Permissions

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