+ Reply to Thread
Results 1 to 3 of 3

If content in column 1 matches, pull all data into one cell separated by a column

Hybrid View

konman726 If content in column 1... 02-02-2012, 08:21 AM
NBVC Re: If content in column 1... 02-02-2012, 08:58 AM
Simon Lloyd Re: If content in column 1... 02-02-2012, 09:02 AM
  1. #1
    Registered User
    Join Date
    02-02-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2010
    Posts
    1

    If content in column 1 matches, pull all data into one cell separated by a column

    Hello,
    Looking for a formula that will look at column 1 and combine the items that match into one column separating details by a comma.
    I've attached a sample excel workbook. Any assistance would be appreciated.

    sample-excel.xlsx

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: If content in column 1 matches, pull all data into one cell separated by a column

    You will need a user-defined function for this.

    Hit Alt+F11 and go to Insert|Module... then paste this code in the editor:

    Function aconcat(a As Variant, Optional sep As String = "") As String
    ' Harlan Grove, Mar 2002
    Dim y As Variant
    
    If TypeOf a Is Range Then
    For Each y In a.Cells
    aconcat = aconcat & y.Value & sep
    Next y
    ElseIf IsArray(a) Then
    For Each y In a
    aconcat = aconcat & y & sep
    Next y
    Else
    aconcat = aconcat & a & sep
    End If
    
    aconcat = Left(aconcat, Len(aconcat) - Len(sep))
    End Function
    Then in the spreadsheet in B2 enter formula:

    =IF(A2="","",SUBSTITUTE(TRIM(aconcat(IF(samplecontent!$A$2:$A$9=A2,samplecontent!$B$2:$B$9," ")))," ",","))

    and confirm it with CTRL+SHIFT+ENTER not just ENTER so that { } brackets appear around the formula... then copy down
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: If content in column 1 matches, pull all data into one cell separated by a column

    You can put this in the result content worksheet code module (right click the worksheet tab and choose view code)
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, MyCell As Range, msg As String
    If Target.Column <> 1 Then Exit Sub
    Set Rng = Sheets("samplecontent").Range("A1:A" & Sheets("samplecontent").Range("A" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
    If MyCell = Target.Value Then
    msg = msg & "," & MyCell.Offset(0, 1).Value
    End If
    Next MyCell
    On Error GoTo Nxt
    Target.Offset(0, 1).Value = Right(msg, Len(msg) - 1)
    Nxt:
    End Sub
    Now when an entry is made in column A of that sheet the result will be filled in!


    Google Me
    Find me located here Simon Lloyd
    and what i'm about Here
    The above is NOT a link to a forum so is NOT against Rule 13
    Last edited by Simon Lloyd; 02-02-2012 at 09:04 AM. Reason: missed a line out of code!

+ 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