I have two opening workbooks. I tried to copy the existing note from one workbook to another if the student record matches. Below is my code. However, the run-time error '91' appears that Object variable or With block variable not set. Any help would be greatly appreciated. 

```
Option Explicit
Public Function SheetFromCodeName(aName As String, Optional wb As Workbook) As Worksheet
Dim sh As Worksheet
For Each sh In wb.Worksheets
    If sh.CodeName = aName Then
       Set SheetFromCodeName = sh
       Exit For
    End If
Next sh
End Function

Sub Note_Transfer()
Dim lastrow As Long: lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim MatchRow As Long
Dim i As Long
Dim sh_old As Worksheet
Dim sh_new As Worksheet

Set sh_old = SheetFromCodeName("Sheet1", Workbooks(Workbooks.Count - 1))
Set sh_new = SheetFromCodeName("Sheet1", Workbooks(Workbooks.Count))

For i = 2 To lastrow
If Cells(i, 25) <> "New student" Then
MatchRow = Application.WorksheetFunction.Match(Cells(i, 23).Value, sh_new.Range("W:W"), 0)
sh_old.Range(Cells(MatchRow, 26), Cells(MatchRow, 32)).Copy _
Destination:=sh_new.Range(Cells(i, 26), Cells(i, 32))

End If
Next

End Sub

```