+ Reply to Thread
Results 1 to 4 of 4

Collate data from Multiple Excel Files

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2011
    Location
    Kolkata, India
    MS-Off Ver
    Office 2007
    Posts
    8

    Collate data from Multiple Excel Files

    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

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Collate data from Multiple Excel Files

    Why dont you attach the file which you are using with this macro? Attach a sample file.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    01-12-2011
    Location
    Kolkata, India
    MS-Off Ver
    Office 2007
    Posts
    8

    Re: Collate data from Multiple Excel Files

    Sample form attached from where the data needs to be collated.

    Hundreds of such files are in a folder which the macro needs to process.
    Attached Files Attached Files

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Collate data from Multiple Excel Files

    In what format should the data be collated? I mean, what should the format for the output?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1