Collate data from Multiple Excel Files

arnabmit

New Member
Joined
Mar 28, 2009
Messages
23
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi,
Am an amateur 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's not working. :eek: :confused:

Please help!!! :biggrin:

Code:
Dim myStrings As Variant
Dim TotalExpectedValues As Long
Dim MyMPath As String
Dim myMFiles() As String
Dim MyMFile As String
Dim fCtr As Long
Dim oRow As Long
Dim fso, txtFile
Dim wb As Workbooks
Dim wks As Worksheet
Dim iCtr As Long

Sub PMSCollate()

    MyMPath = Worksheets(1).TextBox1.Text
    
    If Right(MyMPath, 1) <> "\" Then
        MyMPath = MyMPath & "\"
    End If
    
    On Error Resume Next
        MyMFile = Dir(MyMPath & "*.xls")
    On Error GoTo 0
    
    If MyMFile = "" Then
        MsgBox "No Excel files in this Folder"
        Exit Sub
    End If
    
    TotalExpectedValues = 7
    fCtr = 0
    
    Set wks = Workbooks.Add(1).Worksheets(1)
    wks.Range("a1").Resize(1, TotalExpectedValues).Value = Array("Name", "SAP ID", "Designation", "Supervisor", "Band", "Department", "Score")
    
    
    Do While MyMFile <> ""
        On Error Resume Next
            fCtr = fCtr + 1
            With wks
                oRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            End With
            ReDim Preserve myMFiles(1 To fCtr)
            myMFiles(fCtr) = MyMFile
            Application.ScreenUpdating = False
            Set wb = Workbooks.Open(myMFiles(fCtr), True, True)
            Sheets("Appraisal Form 2012").Select
            myStrings = Array(Range("D4").Value, Range("D5").Value, Range("D6").Value, Range("D7").Value, Range("G5").Value, Range("G6").Value, Range("D33").Value)
            MsgBox myStrings
            For iCtr = LBound(myStrings) To UBound(myStrings)
                wks.Cells(oRow, "A").Offset(0, iCtr).Value = myStrings
            Next iCtr
            MyMFile = Dir()
        On Error GoTo 0
    Loop
    
    wks.UsedRange.Columns.AutoFit
    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
    Range("A1").Select

End Sub
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

arnabmit

New Member
Joined
Mar 28, 2009
Messages
23
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Ok, so I modified the code to quite an extent and almost got it to work!

However, now 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!!!

Code:
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,168
Messages
5,594,632
Members
413,919
Latest member
ZaxAlchemist

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
Top