Ask Experts Questions for FREE Help !
Ask
    chathacc's Avatar
    chathacc Posts: 2, Reputation: 1
    New Member
     
    #1

    Dec 18, 2010, 01:42 PM
    Combine multiple rows into one cell
    Hi,

    I wanted to know if anyone had any suggestions on how to combine data from multiple cells into one cell.

    We have students who complete Math lessons. Our system provides a long list of each lesson completed, but I have to provide a monthly narrative of the lessons completed by each student for the month.

    So I need to combine the lessons completed for each student into on cell. The issue is that a student could have completed anywhere from 1 lesson to 30 lessons during that month so I am not sure how to make the formula smart enough to pull the information automatically for all students.

    There are character limits so I only need to pull the first five lessons completed for each student.

    I have attached an example. Sheet 1 represents the list of lessons completed. Each lesson completed is a row. Some students have multiple rows.

    Sheet 2 is how I want the data to appear for my narrative. The student name in Column A with the first five lessons a student has completed in Column B. It does not have to have the ", and" in between the last two lessons, but it would be a nice touch.
    Attached Files
  1. File Type: xls Example2.xls (28.0 KB, 255 views)
  2. JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Dec 18, 2010, 03:54 PM

    String Concatenation is not a native function to Excel, so it has to be added. I've installed a new function into this worksheet called CONCATIF() and it works like a SUMIF() formula with two additional parameters.

    =ConCatIf(Sheet1!$A$2:$A$55,A2,Sheet1!$B$2:$B$55," , ",TRUE)

    First parameter: Range to evaluate
    Second param: comparison string. (I am assuming you will simply list the students in column A)
    Third param: Range to return values from (These are the lessons)
    Fourth param: The delimiting string for the concatenation, I'm using a comma in this example, but you could use anything.
    Fifth param: (optional) TRUE means eliminate duplicated values, FALSE (or omitted) means string all values, even duplicates.

    This should give you enough info to apply this UDF to your sheet and use it place of that formula of your own... your sample formula of:

    ============

    So in column B you now see ALL the lessons each student completed using this CONCATIF() function matching the names.

    Now, in column C I added a formula to display only the first 5 lessons listed in column B.

    =============
    Here's the UDF code I installed in the worksheet:
    Code:
    Option Explicit
    
    Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
        ' code base by Mike Erickson, MrExcel MVP
        ' used as exactly like SUMIF() with two additional parameters
        ' of delimiter and "no duplicates" as TRUE/FALSE if concatenated values
        ' might include duplicates  ex. =ConcatIf($A$1:$A$10, C1, $B$1:$B$10, ",", True)
    
    Dim i As Long, j As Long
    
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                        stringsRange.Column - compareRange.Column)
        
        For i = 1 To compareRange.Rows.Count
            For j = 1 To compareRange.Columns.Count
                If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                    If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                        ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                    End If
                End If
            Next j
        Next i
        ConcatIf = Replace(ConcatIf, Delimiter & Delimiter, Delimiter)    'eliminates blank values
        If Left(ConcatIf, Len(Delimiter)) = Delimiter Then ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
    End Function
    Attached Files
  3. File Type: xls ConcatIF-Example2.xls (43.5 KB, 332 views)

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Combine text and numbers from different cells into one cell by using a formula [ 2 Answers ]

How can I Combine text and numbers from different cells (these cells are E1 through E590 and F1 through F590, I need to combine E1 with F1, E2 with F2 etc.) into one of the existing cells by using a formula or a function. I am not good with Excel 2003 and do not understand what I have found in...

Formula entry for division of columns in multiple rows [ 3 Answers ]

I'm relative new to Excel having just used it as a lined sheet of paper for listing things. I'm trying to develop an Excel spreadsheet for tracking my car's mileage. Thus far I'm using a basic sheet with 4 columns - designated A=date, B=Miles travelled, C= Gallons used, and D= the computed MPG...

Combine rows of text into one cell [ 12 Answers ]

Hi everyone I hope you are all well! I have an excel problem and it would be much appreciated if someone has the time to take a look... In column A I have product names, in B I have product categories and in C:H the product categories from column B are titles and the name of the subcategory...

How do I combine multiple rar files into 1 video? [ 2 Answers ]

I recently downloaded a movie which came in 8 rar files. I want to combine them into 1 video file. I used winrar but those files are password protected. When I enter the password it opens but when I try to extract 1 of those rar files, it says CRC error after 70% extraction. Tell me a way to...


View more questions Search