So I created a workbook with an UPDATE tab, and a DATA tab. The Update has the button to run the macro, and the DATA tab houses the column Headers and is where the data is brought in.
I am assuming the source sheet is Sheet1, otherwise you need to change it below.
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'==============================================
'Declare Variables
'==============================================
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim LastRow As Long
Dim NameRng As String
Dim DateRng As String
Dim InvoiceNumRng As String
Dim AmountRng As String
Dim SourceSheetName As String
'==============================================
'Optimize Macro Speed
'==============================================
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'==============================================
'Find Folder and Define Variables
'==============================================
NameRng = "C8"
DateRng = "AK4"
InvoiceNumRng = "AK7"
AmountRng = "AL11"
SourceSheetName = "Sheet1"
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then
'Turn these things back on then exit
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Nothing Was Selected. Macro will End."
Exit Sub
End If
'Target File Extension (must include wildcard "*" to capture all files)
myExtension = "*.xl*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
FileCounter = 1
'===================================================================
'Clear Data tab before begining
'===================================================================
ThisWorkbook.Sheets("Data").Range("A2:S1048576").Clear 'Set this range to whatever you need
'This clears whatever range you need to
'I assumed you want to keep Row 1 to keep the headers
LastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Row 'Calculates the first Empty row on the data tab for 2nd column
LastRow = LastRow + 1 'Adds a row so it uses the first EMPTY row
'===================================================================
'This isnt Necessary, only to use the statusbar to indicate how far along you are
'===================================================================
Do While myFile <> ""
FileCount = FileCount + 1
myFile = Dir()
Loop
myFile = Dir(myPath) ' Resets this to the first file in the folder instead of the last
'==============================================
'Loop through each Excel file in folder
'==============================================
Do While myFile <> ""
If myFile <> ThisWorkbook.Name Then
Application.StatusBar = "Importing File [" & FileCounter & " of " & FileCount & "] : " & myFile
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Application.Calculate
'Append data from workbook
ThisWorkbook.Worksheets("Data").Range("A" & LastRow).Value2 = wb.Sheets("Sheet1").Range(NameRng).Value2
ThisWorkbook.Worksheets("Data").Range("B" & LastRow).Value2 = wb.Sheets("Sheet1").Range(DateRng).Value2
ThisWorkbook.Worksheets("Data").Range("C" & LastRow).Value2 = wb.Sheets("Sheet1").Range(InvoiceNumRng).Value2
ThisWorkbook.Worksheets("Data").Range("D" & LastRow).Value2 = wb.Sheets("Sheet1").Range(AmountRng).Value2
'Recalc last row in Thisworkbook
LastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Row + 1
'Close Workbook without Saving
wb.Close SaveChanges:=False
FileCounter = FileCounter + 1
End If
'Get next file name
myFile = Dir
Loop
'==============================================
'Message Box when tasks are completed
'==============================================
MsgBox "Imported " & FileCount & " Files Succesfully"
'==============================================
'Reset Macro Optimization Settings
'==============================================
Application.StatusBar = False
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Bookmarks