That's what I thought, it can't be that simple. 
Below is the corrected code.
Sub AAA()
Dim varrData As Variant
Dim varrHeader As Variant
Dim r As Long, c As Long
Dim oDic As Object
Dim v As Variant
Dim i As Long
Dim varrOldResults As Variant
Dim oDicOldRes As Object
Set oDic = CreateObject("Scripting.Dictionary")
varrData = Worksheets("Base Sheet").ListObjects(1).DataBodyRange.Value
varrHeader = Worksheets("Base Sheet").ListObjects(1).HeaderRowRange.Value
'add teachers (in Keys) and their subjects (in Items) to the Dictionary
For c = 1 To UBound(varrData, 2)
For r = 1 To UBound(varrData)
If Len(varrData(r, c)) > 0 Then
If oDic.Exists(varrData(r, c)) Then
oDic(varrData(r, c)) = oDic(varrData(r, c)) & "|" & varrHeader(1, c)
Else
oDic.Add varrData(r, c), varrHeader(1, c)
End If
End If
Next r
Next c
With Worksheets("Result Sheet")
varrOldResults = .Range("C6").CurrentRegion.Value
'delete rows with old data (if exists)
If UBound(varrOldResults) > 1 Then
.Range(.Cells(7, "C"), .Cells(.Rows.Count, "C").End(xlUp)).EntireRow.Delete
End If
'insert list of teachers
.Range("C7").Resize(oDic.Count).Value = Application.Transpose(oDic.Keys())
'fill the range of 5 columns with "Nil"
.Range("D7").Resize(oDic.Count, 5).Value = "Nil"
'fetch the subjects of the following teachers and put them into the range
For i = 0 To oDic.Count - 1
v = Split(oDic.Items()(i), "|")
.Range("D7").Offset(i).Resize(, UBound(v) + 1).Value = v
Next i
'Restore old teachers' data (if existing)
If UBound(varrOldResults) > 1 Then
Set oDicOldRes = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(varrOldResults)
oDicOldRes.Add varrOldResults(i, 2), Join(Array(varrOldResults(i, 8), varrOldResults(i, 9), varrOldResults(i, 10)), "<!!!>")
Next i
For i = 0 To oDic.Count - 1
If oDicOldRes.Exists(oDic.Keys()(i)) Then
v = Split(oDicOldRes(oDic.Keys()(i)), "<!!!>")
.Range("I7").Offset(i).Resize(, UBound(v) + 1).Value = v
End If
Next i
End If
'insert Sr.No.
With .Range("B7")
.Value = 1
.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=oDic.Count, Trend:=False
End With
End With
End Sub
Artik
Bookmarks