Right I can get it to work by deleting a line or two of code and by changing rng to rDest. This is the code I currently have
Sub FebruaryAddNewPupils()
Application.ScreenUpdating = True
Application.DisplayAlerts = False
Dim Subfolder As String
Dim SubfolderSubjNom As String
Dim SubjFile As String
Dim wbk As Workbook
Dim rFiles As Range, rFree As Range
Dim rData As Range, rDest As Range
Dim iLR As Integer
Dim ws As Worksheet
Sheets("Staff").Select
Subfolder = ActiveSheet.Range("G1").Value
SubfolderSubjNom = Subfolder & "SubjectNominations\"
iLR = ThisWorkbook.Sheets.Range("B" & .Rows.Count).End(xlUp).Row
Set rFiles = ThisWorkbook.Sheets("Staff").Range("B1:B" & iLR)
Set rData = ThisWorkbook.Sheets("Pupils").Range("H10:M12")
For Each rFree In rFiles
SubjFile = SubfolderSubjNom & rFree.Value & ".xlsm"
Set wbk = Workbooks.Open(SubjFile)
Set ws = wbk.Sheets("Nominations")
With ws
.Unprotect
iLR = .Range("A" & .UsedRange.Rows.Count).Row + 1
rData.Copy Destination:=.Range("A" & iLR)
'start re-sorting
'reset last row in destination workbook
iLR = .Range("A" & .Rows.Count).End(xlUp).Row
'define range to be sorted
Set rDest = .Range("A5:W" & iLR)
'sort by columns B,C,D & F relative to top left cell of defined range
rDest.Sort key1:=rDest.Cells(1, 2), key2:=rDest.Cells(1, 3), key3:=rDest.Cells(1, 4), key4:=rDest(1, 6), Header:=xlYes
'end re-sorting
.Range("A2").Select
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End With
wbk.Save
wbk.Close
Next rFree
End Sub
The bits it still isn't liking are
key1:=rDest.Cells(1, 2), key2:=rDest.Cells(1, 3), key3:=rDest.Cells(1, 4), key4:=rDest(1, 6),
It is OK if i delete the key2/key3/key4 bits; but doesn't like it when they are in there. I can't see why, as we have clearly defined rDest to have at least six columns
The other bit it still isn't liking is the very first instance of iLR i.e.
iLR = ThisWorkbook.Sheets.Range("B" & .Rows.Count).End(xlUp).Row
where it highights .Rows and tells me its a compile error: invalid or unqualified reference
Bookmarks