Function compareCourses() As String
Dim RqdCourses() As String
Dim Rng As Range
Dim x As Long
[COLOR=#00ff00]'what row is the function in[/COLOR]
x = Application.Caller.Row
[COLOR=#00ff00]'find the last used column in row one.[/COLOR]
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
[COLOR=#00ff00]'if there are headers in the two Rpt columns, you'll need this to find the last course, if not, use LastCol in it's place.[/COLOR]
lastCourse = Cells(1, lastCol - 2).End(xlToLeft).Column
[COLOR=#00ff00]'turn off events[/COLOR]
Application.EnableEvents = False
[COLOR=#00ff00]'set the beginning text[/COLOR]
temp = "Rpt "
[COLOR=#00ff00]'put the required courses into an array[/COLOR]
RqdCourses = Split(Replace(Cells(x, lastCol - 1), "Rpt ", ""), ", ")
[COLOR=#00ff00] 'loop thru each required course[/COLOR]
For i = 0 To UBound(RqdCourses)
[COLOR=#00ff00] 'if there is something in that item[/COLOR]
If Trim(RqdCourses(i)) <> "" Then
[COLOR=#00ff00] 'set the columns to find the course names[/COLOR]
With Sheets("Sheet1").Range(Cells(1, 3), Cells(1, lastCourse))
[COLOR=#00ff00] 'search for the course name[/COLOR]
Set Rng = .Find(What:=RqdCourses(i), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
[COLOR=#00ff00] 'where the course name is found[/COLOR]
If Not Rng Is Nothing Then
[COLOR=#00ff00] 'if the course name is Abs or a grade of F. Add more Or statements for other failing grades.[/COLOR]
If Rng.Offset(x - 1, 3) = "Abs" Or Rng.Offset(x - 1, 3) = "F" Then
[COLOR=#00ff00] 'write the course name[/COLOR]
temp = temp & Rng.Value & ", "
End If
End If
End With
End If
Next
[COLOR=#00ff00]'get rid of the last comma and space[/COLOR]
If Right(temp, 2) = ", " Then temp = Left(temp, Len(temp) - 2)
[COLOR=#00ff00]'if there are no courses listed[/COLOR]
If Len(temp) < 5 Then
[COLOR=#00ff00] 'return Nil[/COLOR]
compareCourses = "Nil"
Else
[COLOR=#00ff00] 'otherwise return the course names.[/COLOR]
compareCourses = temp
End If
[COLOR=#00ff00]'turn events back on[/COLOR]
Application.EnableEvents = True
End Function