Try this code
Copy the Excel VBA code![]()
Option Explicit Sub cons_data() Dim Master As Workbook Dim sourceBook As Workbook Dim sourceData As Worksheet Dim CurrentFileName As String Dim myPath As String Dim sname as string Application.ScreenUpdating = False Application.DisplayAlerts=false 'The folder containing the files to be recap'd myPath = "D:\Test" 'Finds the name of the first file of type .xls in the current directory CurrentFileName = Dir(myPath & "\*.csv") 'Create a workbook for the recap report Set Master = ThisWorkbook Do Workbooks.Open (myPath & "\" & CurrentFileName) Set sourceBook = Workbooks(CurrentFileName) Set sourceData = sourceBook.Worksheets(1) With sourceData sname = Left(CurrentFileName, Len(CurrentFileName) - 4) & "Data" Master.Worksheets.Add(after:=Master.Worksheets(Master.Worksheets.Count)).Name = sname .Cells.Copy Master.Worksheets(sname).Range("A1") End With sourceBook.Close 'Calling DIR w/o argument finds the next .xlsx file within the current directory. CurrentFileName = Dir() Loop While CurrentFileName <> "" msgbox "Done" Application.displayalerts=True Application.ScreenUpdating = True End Sub
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
Bookmarks