VBA code that will check all files in a directory, copy the contents of the first sheet, and paste it on the first sheet of the main excel file

tr0jance

New Member
Joined
Feb 27, 2018
Messages
2
Hello Mr Excel team.

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.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Sorry If I came out rude, first of all thank you for accepting me on this forums, secondly if you could help me with my request I'll greatly appreciate it.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top