Hi,
Am amateur at best in VBA. I need a Macro which will collate value of cells (not formulas) from specific cells in each excel files in a folder, and then collate them in a separate excel file.
I have put together a code from bits and pieces from my earlier practice projects, however, as expected, it was not working. So after a lot of head scratching I modified the code to quite an extent and almost got it to work!
However, I am getting only the value of cell B5 from each file in the folder in all the 7 columns of BaseWks...
Am pretty much sure that my mistake is in the Range syntax or something like it...
Please Help!!!
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 rnum As Long
Sub MergeAllWorkbooks()
' Change this to the path\folder location of your files.
MyMPath = Worksheets(1).TextBox1.Text
' 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 & "*.xls")
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
.ScreenUpdating = False
.EnableEvents = False
End With
' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Range("a1").Resize(1, 7).Value = Array("SAP ID", "Name", "Designation", "Supervisor", "Band", "Department", "Score")
rnum = 1
' 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.
With mybook.Worksheets(1)
Set sourceRange = .Range("D5,D4,D6,D7,G5,G6,D33,G33")
End With
On Error GoTo 0
If Not sourceRange Is Nothing Then
' Set the destination range.
Set destrange = BaseWks.Range("A" & rnum + 1)
' Copy the values from the source range
' to the destination range.
With sourceRange
Set destrange = destrange.Resize(1, 7)
End With
destrange.Value = sourceRange.Value
rnum = rnum + 1
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Range("A1:G1").Select
Selection.Font.Bold = True
Columns("A:G").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Bookmarks