I think this code is written by Ron. I have adjusted some of the lines to suite my needs. The code loops correctly through all files on the folder, but copies only the range from the first book. It loops through the rest of the folder, but does not copy anything. I have range values in most workbooks, but the code does not copy. My books have a mixture of file extenstions, xlsx, xlsm.
Any idea, or suggestion would be much appreciated!
ub MergeAllWorkbooks()
Dim SummarySheet As Worksheet, FolderPath As String
Dim NR As Long, FileName As String, WorkBk As Workbook, SourceRange As Range, DestRange As Range
Application.ScreenUpdating = 0: Application.DisplayAlerts = 0: Application.EnableEvents = 0
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
FolderPath = "C:\Marcotest\" ' Modify this folder path to point to the files you want to use.
NR = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
Do While FileName <> "" ' Loop until Dir returns an empty string.
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NR).Value = FileName
' Set the source range to be A9 through C9.Modify this range for your workbooks.
Set SourceRange = WorkBk.Worksheets(1).Range("A1:Z1000")
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NR)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
NR = NR + DestRange.Rows.Count ' Increase NR so that we know where to copy data next.
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
SummarySheet.Columns.AutoFit ' Call AutoFit on the destination sheet so that all data is readable.
End Sub
Bookmarks