Hi, ids,
The problem I think is that I am trying to use 1 variable (sourcerange) to do two different things
To be honest: I think you donīt really understand what the code is doing and maybe choose it because you believe it to be fitting for your problem but the code may be simplified due to your requests. Maybe itīs a good idea to put the cursor isnoide the procedure and use F8 to single step through the code in order to view what the code is supposed to do.
The problem about the filename is that you either have to do it every other column in order to get the proper name for each set of data. Or you put it above the data (like it is in my version). And I donīt believe it to be very efficient to create an array with the file names and only after that get on opening the files and work on them.
Sub MergeAllWorkbooks()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim sourceRange1 As Range, destrange1 As Range
Dim rnum As Long, CalcMode As Long
Dim lngCol As Long
' Change this to the path\folder location of your files.
MyPath = "C:\temp1"
' Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
' If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
' Set worksheet for information to be merged to.
Set BaseWks = Worksheets(1)
rnum = 2
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
' Change this range to fit your own needs.
Set sourceRange = mybook.Worksheets(6).Range("b5:b34")
Set sourceRange1 = mybook.Worksheets(4).Range("b5:b34")
If Not sourceRange Is Nothing Then
' Copy the file name in column A.
lngCol = BaseWks.Cells(1, Columns.Count).End(xlToLeft).Column + 1
If BaseWks.Cells(1, 1).Value = "" Then
lngCol = 1
End If
BaseWks.Cells(rnum, lngCol).Value = MyFiles(FNum)
' Set the destination range.
With BaseWks
Set destrange = .Cells(rnum, lngCol).Resize(30, 1)
Set destrange1 = .Cells(rnum + 30, lngCol).Resize(30, 1)
End With
' Copy the values from the source range
' to the destination range. Overwrite is here
destrange.Value = sourceRange.Value
destrange1.Value = sourceRange1.Value
' End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.Calculation = CalcMode
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Ciao,
Holger
Bookmarks