'How to pull data from multiple workbooks in a folder?

I have hundreds of Excel (Microsoft® Excel® for Microsoft 365 MSO (16.0.14326.20702) 32-bit ) files in a folder which have one sheet in common.

For example- Let's consider the sheet as "data".
I want to pull specific cells (C2:C15) out of each of them and transpose them into a separate "masterfile".

This code runs unsuccessfully.

Sub ExtractData()

Dim masterfile As Workbook
Dim wb As Workbook
Dim directory As String
Dim fileName As String
Dim NextRow As Long

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set masterfile = ThisWorkbook
directory = masterfile.Worksheets("Sheet1").Range("E1")
fileName = Dir(directory & "*.xl??")

Do While fileName <> ""
    If fileName <> ThisWorkbook.Name Then
        Set wb = Workbooks.Open(directory & fileName)
        wb.Worksheets("data").Range("C2:C15").Copy
        masterfile.Activate
        NextRow = Cells(Rows.Count, "C").End(xlUp).Row + 1
        Worksheets("Sheet1").Range("C" & NextRow).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        wb.Close savechanges:=False
    End If
    fileName = Dir
Loop

Application.DisplayAlerts = True
Application.ScreenUpdating = True

MsgBox "Data has been Compiled, Please Check!"

End Sub


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source