+ Reply to Thread
Results 1 to 6 of 6

Concatenate

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-12-2004
    Posts
    329

    Concatenate

    Let’s say I have 2 columns, A and B:

    ColA ColB
    1101 a
    1101
    1101 b
    1101 12
    1103 c
    1103 d
    1103 e
    1111 f
    1106 g
    1106
    1106 h
    1106
    1106 i


    I want to concatenate ColB so it will look like this:

    ColA ColB
    1101 a,b,12
    1103 c,d,e
    1111 f
    1106 g,h,i


    Is there a formula that can do this? Approach involving manual procedure (ie. copy and paste, etc) is not an option.

    Thank you

  2. #2
    Miguel Zapico
    Guest

    RE: Concatenate

    You can use a custom formula like this:
    Function ConcatenateByCode(strInput As String)
    Dim rngData, rngCode As Range
    Dim strResult As String

    Application.Volatile
    Set rngData = ActiveSheet.Range("B1:B13")
    Set rngCode = ActiveSheet.Range("A1:A13")

    For Each cell In rngCode.Cells
    If cell.Value = strInput Then
    If rngData.Cells(cell.Row, 1).Value <> "" Then
    strResult = strResult & rngData.Cells(cell.Row, 1).Value & ","
    End If
    End If
    Next

    strResult = Left(strResult, Len(strResult) - 1)
    ConcatenateByCode = strResult
    End Function

    Change the ranges A1:A13 and B1:B13 to match your case. Also, if you want
    to use it on a different sheet you can hardcode the name of the sheet on
    those ranges, or accept it as an input.
    To use it, just enter in column B of your desired result:
    =ConcatenateByCode(A1)

    Hope this helps,
    Miguel.

    "Morrigan" wrote:

    >
    > Let’s say I have 2 columns, A and B:
    >
    > ColA ColB
    > 1101 a
    > 1101
    > 1101 b
    > 1101 12
    > 1103 c
    > 1103 d
    > 1103 e
    > 1111 f
    > 1106 g
    > 1106
    > 1106 h
    > 1106
    > 1106 i
    >
    >
    > I want to concatenate ColB so it will look like this:
    >
    > ColA ColB
    > 1101 a,b,12
    > 1103 c,d,e
    > 1111 f
    > 1106 g,h,i
    >
    >
    > Is there a formula that can do this? Approach involving manual
    > procedure (ie. copy and paste, etc) is not an option.
    >
    > Thank you
    >
    >
    > --
    > Morrigan
    > ------------------------------------------------------------------------
    > Morrigan's Profile: http://www.excelforum.com/member.php...fo&userid=7094
    > View this thread: http://www.excelforum.com/showthread...hreadid=544864
    >
    >


  3. #3
    Forum Contributor
    Join Date
    03-12-2004
    Posts
    329
    I am getting "#NAME?", seems like Excel is not recognizing the function after I pasted the code in VBA. Any idea? (More explanation the better, I would not even consider myself a beginner in VBA) Furthermore, I would like to avoid using fixed range; for instance, if I add a row I would like the function to change the range automatically.

    Btw in "rngData.Cells(cell.Row, 1).Value", what does "Cells(cell.Row, 1)" mean?

    Thank you for the input


    Quote Originally Posted by Miguel Zapico
    You can use a custom formula like this:
    Function ConcatenateByCode(strInput As String)
    Dim rngData, rngCode As Range
    Dim strResult As String

    Application.Volatile
    Set rngData = ActiveSheet.Range("B1:B13")
    Set rngCode = ActiveSheet.Range("A1:A13")

    For Each cell In rngCode.Cells
    If cell.Value = strInput Then
    If rngData.Cells(cell.Row, 1).Value <> "" Then
    strResult = strResult & rngData.Cells(cell.Row, 1).Value & ","
    End If
    End If
    Next

    strResult = Left(strResult, Len(strResult) - 1)
    ConcatenateByCode = strResult
    End Function

    Change the ranges A1:A13 and B1:B13 to match your case. Also, if you want
    to use it on a different sheet you can hardcode the name of the sheet on
    those ranges, or accept it as an input.
    To use it, just enter in column B of your desired result:
    =ConcatenateByCode(A1)

    Hope this helps,
    Miguel.

    "Morrigan" wrote:

    >
    > Let’s say I have 2 columns, A and B:
    >
    > ColA ColB
    > 1101 a
    > 1101
    > 1101 b
    > 1101 12
    > 1103 c
    > 1103 d
    > 1103 e
    > 1111 f
    > 1106 g
    > 1106
    > 1106 h
    > 1106
    > 1106 i
    >
    >
    > I want to concatenate ColB so it will look like this:
    >
    > ColA ColB
    > 1101 a,b,12
    > 1103 c,d,e
    > 1111 f
    > 1106 g,h,i
    >
    >
    > Is there a formula that can do this? Approach involving manual
    > procedure (ie. copy and paste, etc) is not an option.
    >
    > Thank you
    >
    >
    > --
    > Morrigan
    > ------------------------------------------------------------------------
    > Morrigan's Profile: http://www.excelforum.com/member.php...fo&userid=7094
    > View this thread: http://www.excelforum.com/showthread...hreadid=544864
    >
    >

  4. #4
    Miguel Zapico
    Guest

    Re: Concatenate

    The function should be located in a module, you can create one easily from
    the Visual Basic editor (ALT+F11 on Excel) and going to the menu
    Insert->Module. Paste the code there and it should work.
    About the range, the range that is hardcoded is the lookup table. You can
    fix it to the greatest value you expect, but that may affect the performance.
    There are ways of making the dependant of the region size, but I wanted to
    keep the function simple.
    The Cells(cell.Row,1) is the cell of the values range where the iteration
    is, the value is taken from the B column if the cell in column A has the same
    value as the input string.

    Miguel.

    "Morrigan" wrote:

    >
    > I am getting "#NAME?", seems like Excel is not recognizing the function
    > after I pasted the code in VBA. Any idea? (More explanation the
    > better, I would not even consider myself a beginner in VBA)
    > Furthermore, I would like to avoid using fixed range; for instance, if
    > I add a row I would like the function to change the range
    > automatically.
    >
    > Btw in "rngData.Cells(cell.Row, 1).Value", what does "Cells(cell.Row,
    > 1)" mean?
    >
    > Thank you for the input
    >
    >
    > Miguel Zapico Wrote:
    > > You can use a custom formula like this:
    > > Function ConcatenateByCode(strInput As String)
    > > Dim rngData, rngCode As Range
    > > Dim strResult As String
    > >
    > > Application.Volatile
    > > Set rngData = ActiveSheet.Range("B1:B13")
    > > Set rngCode = ActiveSheet.Range("A1:A13")
    > >
    > > For Each cell In rngCode.Cells
    > > If cell.Value = strInput Then
    > > If rngData.Cells(cell.Row, 1).Value <> "" Then
    > > strResult = strResult & rngData.Cells(cell.Row, 1).Value & ","
    > > End If
    > > End If
    > > Next
    > >
    > > strResult = Left(strResult, Len(strResult) - 1)
    > > ConcatenateByCode = strResult
    > > End Function
    > >
    > > Change the ranges A1:A13 and B1:B13 to match your case. Also, if you
    > > want
    > > to use it on a different sheet you can hardcode the name of the sheet
    > > on
    > > those ranges, or accept it as an input.
    > > To use it, just enter in column B of your desired result:
    > > =ConcatenateByCode(A1)
    > >
    > > Hope this helps,
    > > Miguel.
    > >
    > > "Morrigan" wrote:
    > >
    > > >
    > > > Let’s say I have 2 columns, A and B:
    > > >
    > > > ColA ColB
    > > > 1101 a
    > > > 1101
    > > > 1101 b
    > > > 1101 12
    > > > 1103 c
    > > > 1103 d
    > > > 1103 e
    > > > 1111 f
    > > > 1106 g
    > > > 1106
    > > > 1106 h
    > > > 1106
    > > > 1106 i
    > > >
    > > >
    > > > I want to concatenate ColB so it will look like this:
    > > >
    > > > ColA ColB
    > > > 1101 a,b,12
    > > > 1103 c,d,e
    > > > 1111 f
    > > > 1106 g,h,i
    > > >
    > > >
    > > > Is there a formula that can do this? Approach involving manual
    > > > procedure (ie. copy and paste, etc) is not an option.
    > > >
    > > > Thank you
    > > >
    > > >
    > > > --
    > > > Morrigan
    > > >

    > > ------------------------------------------------------------------------
    > > > Morrigan's Profile:

    > > http://www.excelforum.com/member.php...fo&userid=7094
    > > > View this thread:

    > > http://www.excelforum.com/showthread...hreadid=544864
    > > >
    > > >

    >
    >
    > --
    > Morrigan
    > ------------------------------------------------------------------------
    > Morrigan's Profile: http://www.excelforum.com/member.php...fo&userid=7094
    > View this thread: http://www.excelforum.com/showthread...hreadid=544864
    >
    >


  5. #5
    Forum Contributor
    Join Date
    03-12-2004
    Posts
    329
    Ahh~inserting a module is what I didn't do. Super, thanks again!

  6. #6
    Forum Contributor
    Join Date
    03-12-2004
    Posts
    329
    I just realized I cannot use fixed range since I have 5 different tables. Thus, I have 5 different range of cells for rngCode and rngData. Using fixed range will lead to 5 custom functions, which can be confusing as time goes by. I was trying to modify the code so that rngCode and rngData are inputs, but no luck with my lack of VBA experience. The following is what I tried:

    Function ConcatenateByCode(strInput As String, rngCode As Range, rngData As Range)
    Dim strResult As String

    Application.Volatile
    For Each cell In rngCode.Cells
    If cell.Value = strInput Then
    If rngData.Cells(cell.Row, 1).Value <> "" Then
    strResult = strResult & rngData.Cells(cell.Row, 1).Value & ","
    End If
    End If
    Next

    strResult = Left(strResult, Len(strResult) - 1)
    ConcatenateByCode = strResult
    End Function


    Any help is appreciated, thank you.



    Quote Originally Posted by Miguel Zapico
    The function should be located in a module, you can create one easily from
    the Visual Basic editor (ALT+F11 on Excel) and going to the menu
    Insert->Module. Paste the code there and it should work.
    About the range, the range that is hardcoded is the lookup table. You can
    fix it to the greatest value you expect, but that may affect the performance.
    There are ways of making the dependant of the region size, but I wanted to
    keep the function simple.
    The Cells(cell.Row,1) is the cell of the values range where the iteration
    is, the value is taken from the B column if the cell in column A has the same
    value as the input string.

    Miguel.

    "Morrigan" wrote:

    >
    > I am getting "#NAME?", seems like Excel is not recognizing the function
    > after I pasted the code in VBA. Any idea? (More explanation the
    > better, I would not even consider myself a beginner in VBA)
    > Furthermore, I would like to avoid using fixed range; for instance, if
    > I add a row I would like the function to change the range
    > automatically.
    >
    > Btw in "rngData.Cells(cell.Row, 1).Value", what does "Cells(cell.Row,
    > 1)" mean?
    >
    > Thank you for the input
    >
    >
    > Miguel Zapico Wrote:
    > > You can use a custom formula like this:
    > > Function ConcatenateByCode(strInput As String)
    > > Dim rngData, rngCode As Range
    > > Dim strResult As String
    > >
    > > Application.Volatile
    > > Set rngData = ActiveSheet.Range("B1:B13")
    > > Set rngCode = ActiveSheet.Range("A1:A13")
    > >
    > > For Each cell In rngCode.Cells
    > > If cell.Value = strInput Then
    > > If rngData.Cells(cell.Row, 1).Value <> "" Then
    > > strResult = strResult & rngData.Cells(cell.Row, 1).Value & ","
    > > End If
    > > End If
    > > Next
    > >
    > > strResult = Left(strResult, Len(strResult) - 1)
    > > ConcatenateByCode = strResult
    > > End Function
    > >
    > > Change the ranges A1:A13 and B1:B13 to match your case. Also, if you
    > > want
    > > to use it on a different sheet you can hardcode the name of the sheet
    > > on
    > > those ranges, or accept it as an input.
    > > To use it, just enter in column B of your desired result:
    > > =ConcatenateByCode(A1)
    > >
    > > Hope this helps,
    > > Miguel.
    > >
    > > "Morrigan" wrote:
    > >
    > > >
    > > > Let’s say I have 2 columns, A and B:
    > > >
    > > > ColA ColB
    > > > 1101 a
    > > > 1101
    > > > 1101 b
    > > > 1101 12
    > > > 1103 c
    > > > 1103 d
    > > > 1103 e
    > > > 1111 f
    > > > 1106 g
    > > > 1106
    > > > 1106 h
    > > > 1106
    > > > 1106 i
    > > >
    > > >
    > > > I want to concatenate ColB so it will look like this:
    > > >
    > > > ColA ColB
    > > > 1101 a,b,12
    > > > 1103 c,d,e
    > > > 1111 f
    > > > 1106 g,h,i
    > > >
    > > >
    > > > Is there a formula that can do this? Approach involving manual
    > > > procedure (ie. copy and paste, etc) is not an option.
    > > >
    > > > Thank you
    > > >
    > > >
    > > > --
    > > > Morrigan
    > > >

    > > ------------------------------------------------------------------------
    > > > Morrigan's Profile:

    > > http://www.excelforum.com/member.php...fo&userid=7094
    > > > View this thread:

    > > http://www.excelforum.com/showthread...hreadid=544864
    > > >
    > > >

    >
    >
    > --
    > Morrigan
    > ------------------------------------------------------------------------
    > Morrigan's Profile: http://www.excelforum.com/member.php...fo&userid=7094
    > View this thread: http://www.excelforum.com/showthread...hreadid=544864
    >
    >

+ 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