Thanks for your input guys. I have done some reading, and although I could have gone for the pivot table option, I do not need to access this information for reporting basis - it's simply a print out for my bosses. So with the power of arrays I have significantly streamlined the code, and it completes in less than a minute. Still not rocket speed, but more than enough for what I'll be using it for.
Thank you for all your help. Here is what I ended up with:
rCnt = Sheet1.Range("B6500").End(xlUp).Row
ccArr = Sheet1.Range("A2:A" & rCnt)
For i = LBound(ccArr) To UBound(ccArr)
If i > 1 Then ''' works on every record after the first one
gapCount = Application.WorksheetFunction.Max(subCount, appCount) ''' calculate whether more subs or apps so as to know where to put the next student details
RowCount = Sheet42.Range("A2").CurrentRegion.Rows.Count + 1
Sheet42.Range("A" & RowCount).Value = Application.WorksheetFunction.VLookup(ccArr(i, 1), Range("StudentData"), 5, False) ''' GET THE NAME FROM CCODE
Else: gapCount = 0 ''' ONLY ON FIRST RECORD
RowCount = 2
Sheet42.Range("A2").Value = Application.WorksheetFunction.VLookup(ccArr(i, 1), Range("StudentData"), 5, False)
End If
With Application.WorksheetFunction
On Error Resume Next
appRow = 0 ''' Reset the row counts - PARTICULARLY RELEVANT IF NO APPLICATIONS MADE
subRow = 0
appRow = .Match(ccArr(i, 1), Sheet33.Range("A:A"), 0)
subRow = .Match(ccArr(i, 1), Sheet2.Range("A:A"), 0)
appCount = .CountIf(apprange, ccArr(i, 1))
subCount = .CountIf(subrange, ccArr(i, 1))
End With
subArr = Sheet2.Range("C" & subRow & ":G" & subRow + subCount - 1)
If IsError(Sheet33.Range("B" & appRow & ":D" & appRow + appCount - 1)) Then ''' APPLICATIONS ARRAY IS LIKELY TO BE A BIT DODGY IF THERE IS NO APPLICATIONS MADE
Erase appArr ''' RESETS THE ARRAY TO NOTHING
Else: appArr = Sheet33.Range("B" & appRow & ":D" & appRow + appCount - 1)
End If
Sheet42.Range("B" & RowCount).Resize(UBound(subArr, 1), UBound(subArr, 2)) = subArr
Sheet42.Range("G" & RowCount).Resize(UBound(appArr, 1), UBound(appArr, 2)) = appArr
''' ADD LINE UNDER EACH STUDENT
If gapCount = 0 Then
Rows(i + Application.WorksheetFunction.Max(subCount, appCount)).Select
Else: Rows(RowCount + Application.WorksheetFunction.Max(subCount, appCount) - 1).Select
End If
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.249946592608417
.Weight = xlMedium
End With
Next
Cells.Select
Selection.Columns.AutoFit
Application.ScreenUpdating = True
lcell = Sheet42.UsedRange.Rows.Count
reprange = "A2:I" & lcell
Range(reprange).Select
ActiveSheet.PageSetup.PrintArea = reprange
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.CenterHeader = "&""-,Bold""&14Application Grid - " & Format(Date, "dd/mm/yy")
.Orientation = xlLandscape
.PaperSize = xlPaperA4
End With
Application.PrintCommunication = True
Sheet42.PrintPreview
Bookmarks