Consulting

Results 1 to 9 of 9

Thread: CONCATENATE

  1. #1
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location

    CONCATENATE

    hi
    i need some help.
    i have to columns "A" AND "F"
    in column "A" is the first name and in column "F" there is
    a last name of a person in column "H" i need to bee writen first and last name together with comma separratior.
    I need a macro to do this because the length of the data
    changes

    thanks
    oleg

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi oleg,

    What code have you written for this so far?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    I did not write the code
    at this moment i am using
    =CONCATENATE(A2,"," & F2)
    i rtied to record the macro but i did not know how to
    insert a formula whike recording


    Oleg

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Oleg,

    The macro recorder won't do this sort of work for you. However, you've been given help in the past with code that loops through the rows in a column to do something. See:
    http://www.vbaexpress.com/forum/showthread.php?t=34334)
    There are also many similar examples here at VBA Express. Using the principles in the code from the above thread as a basis, you should be able to develop something that does a similar thing for your existing worksheet. All you need to do is to tell Excel to put the values from columns A & F on the current row into Column H, with a comma separator.

    This is not at all difficult to do, requiring as few as 4 lines of code to do the processing.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Use the code below:
    At the moment, it will add the result in the sheet1, column G

    [VBA]Sub Concatenate()
    Dim LastRow As Long
    LastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    With Sheet1
    For i = 2 To LastRow
    .Range("G" & i).FormulaR1C1 = "=CONCATENATE(RC[-6],"" , "",RC[-1])"
    Next i
    End With
    End Sub
    [/VBA]

    The main problem with the recorded code is it will never use looping. So you can use the forum or some reference books to learn them on your own.
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Concatenate()
    Dim LastRow As Long

    LastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Sheet1").Range("G2").Resize(LastRow - 1)

    .Formula = "A2&"", ""&F2"
    .Value = .Value
    End With
    End Sub
    [/vba]
    Last edited by Bob Phillips; 12-16-2010 at 04:03 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    As I said, as few as four lines are needed for the processing:
    [VBA]Dim i As Long
    For i = 2 To ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
    ActiveSheet.Range("H" & i).Value = ActiveSheet.Range("A" & i).Value & ", " & ActiveSheet.Range("F" & i).Value
    Next[/VBA]
    An efficient fully-fledged sub would be:
    [VBA]Sub Concatenate()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveSheet
    For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("H" & i).Value = .Range("A" & i).Value & ", " & .Range("F" & i).Value
    Next
    End With
    Application.ScreenUpdating = True
    End Sub[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    And as I showed, there is absolutely no need to have a loop.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi xld,

    No dispute there.

    The whole point of my original post, though, was to get Oleg to apply some of the logic from previous help he's been given to solving the problem, instead of simply having someone here doing the coding for him.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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