Hello Mr Excel team.
I have this code
I've been using this code to get the name of the excel file and the name of each sheet.
What I want is to use the same code or another code to get all the files in a directory, copy the contents of their first sheet, and paste them on the main file. I want them to append as well.
I have this code
Code:
Sub FetchName()
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open ("c\text.xlsb")
directory = "c\test\test\"
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
i = i + 1
j = 1
Cells(i, 1) = fileName
Workbooks.Open (directory & fileName)
For Each sheet In Workbooks(fileName).Worksheets
Workbooks("test.xlsb").Worksheets(1).Cells(i, j).Value = sheet.Range("A:J").Value
j = j + 1
Next sheet
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I've been using this code to get the name of the excel file and the name of each sheet.
What I want is to use the same code or another code to get all the files in a directory, copy the contents of their first sheet, and paste them on the main file. I want them to append as well.