This code is suppose to pull data from other worksheets and combine them all into one. Each workbook that this macro is looking through has an "invoice" sheet and "data" sheet. All I need are the records on the "data" sheet. When I step through the code and get to For Each WSN In WBN.Worksheets("Data") I get the Run Time Error. Can anybody help? This code is from Mr Excel Live Lessons.
Sub CombineFiles()
Dim WBO As Workbook ' original workbook
Dim WBN As Workbook ' individual data workbooks
Dim WSL As Worksheet ' List of files worksheet
Dim WSG As Worksheet ' data gathering worksheet
Dim WSN As Worksheet
'Define Object variables
Set WBO = ThisWorkbook
Set WSL = WBO.Worksheets("List")
Set WSG = WBO.Worksheets("Gather")
Application.DisplayAlerts = False
'Clear out any previous data on WSD, but leave the headings
WSG.Cells(2, 1).Resize(Rows.Count - 1, Columns.Count).Clear
NextRow = 2
'Loop through all the files on WSL
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To FinalRow
ThisFile = WSL.Cells(i, 1)
'Open a file
Set WBN = Workbooks.Open(Filename:=ThisFile)
For Each WSN In WBN.Worksheets("Data")
'Last row in this file? Rowcount is 1 less
LastRow = WSN.Cells(Rows.Count, 1).End(xlUp).Row
RowCount = LastRow - 1
'Copy from 5 to last row over to column B
WSN.Cells(2, 1).Resize(RowCount, 17).Copy _
Destination:=WSG.Cells(NextRow, 1)
'Replicate the department from A2 to column A in WSD
'WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value _
' = WSN.Range("A2")
'Set up the new NextRow
NextRow = NextRow + RowCount
Next WSN
'Close WSN don't save
WBN.Close SaveChanges:=False
Next i
Application.DisplayAlerts = True
End Sub
Sub CombineFiles()
Dim WBO As Workbook ' original workbook
Dim WBN As Workbook ' individual data workbooks
Dim WSL As Worksheet ' List of files worksheet
Dim WSG As Worksheet ' data gathering worksheet
Dim WSN As Worksheet
'Define Object variables
Set WBO = ThisWorkbook
Set WSL = WBO.Worksheets("List")
Set WSG = WBO.Worksheets("Gather")
Application.DisplayAlerts = False
'Clear out any previous data on WSD, but leave the headings
WSG.Cells(2, 1).Resize(Rows.Count - 1, Columns.Count).Clear
NextRow = 2
'Loop through all the files on WSL
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To FinalRow
ThisFile = WSL.Cells(i, 1)
'Open a file
Set WBN = Workbooks.Open(Filename:=ThisFile)
For Each WSN In WBN.Worksheets("Data")
'Last row in this file? Rowcount is 1 less
LastRow = WSN.Cells(Rows.Count, 1).End(xlUp).Row
RowCount = LastRow - 1
'Copy from 5 to last row over to column B
WSN.Cells(2, 1).Resize(RowCount, 17).Copy _
Destination:=WSG.Cells(NextRow, 1)
'Replicate the department from A2 to column A in WSD
'WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value _
' = WSN.Range("A2")
'Set up the new NextRow
NextRow = NextRow + RowCount
Next WSN
'Close WSN don't save
WBN.Close SaveChanges:=False
Next i
Application.DisplayAlerts = True
End Sub