I can't find the error in this code (from this answer) The macro is triggered when a button in the already opened "Pub" file is clicked All works until the copy from another file. Where is my error? Thank you in advance
Sub Foo()
Dim S As Worksheet, MM As Worksheet
Dim Pub As Workbook, Net As Workbook
Dim folderPath As String
Dim Boat As String, SPub As String, SNet As String, SNetinPub As String
Dim LastRow As Long
Set Pub = ActiveWorkbook
Set S = Sheets("Main Sheet")
Boat = S.Cells(4, 9).Value
Set MM = Sheets(Boat) 'retrieve and select a sheet in the current "Pub" file
With MM 'Find the last filled row
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
End With
S.Cells(1, 7) = LastRow
S.Cells(2, 7) = LastRow - 5 + 27
SPub = "A6:F" & LastRow
SNet = "F6:F" & LastRow
SNetinPub = "G28:G" & S.Cells(2, 7).Value
MsgBox SNetinPub 'Check: it is really correct
Sheets(Boat).Range(SPub).Copy
With ActiveSheet.Range("A28")
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With 'This copy all inside the "Pub file" works
'open the "Net" file in the same path
folderPath = Application.ActiveWorkbook.Path
Set Net = Workbooks.Add(folderPath & "\Net.xls")
'Method 1
Net.Sheets(Boat).Range(SNet).Copy
Pub.S.Range(SNetinPub).PasteSpecial 'ERROR HERE
'Method 2
'Net.Sheets(Boat).Range(SNet).Copy
'With Pub.S.Range("G28")
' .PasteSpecial xlPasteFormats 'OR ERROR HERE
' .PasteSpecial xlPasteValues
'End With
Net.Close True 'I don't wont to close Pub file after the macro
End Sub