Excel Macro for copying data from multiple files in folder to one sheet

Karanam_teja

New Member
Joined
Feb 26, 2017
Messages
3
Hi,

I am looking for macro which can help me in copying data of row number 2 to 180 from all 240 excel files(in a folder named 'Data') with the sheet named 'Information' in each file to a single excel Sheet in a separate Master excel file. The data shall be copied to a single sheet in master file, one after another simultaneously so on.


Please help me in this regard, as i was very new to Macros.

Regards,
KT
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this macro in your Master workbook,changing the workbook filespec where shown. The rows are copied to the first sheet in the Master workbook.
Code:
Public Sub Copy_Rows_From_Workbooks2()

    Dim fileSpec As String, folderPath As String, fileName As String
    Dim destCell As Range, r As Long
    
    fileSpec = "C:\Data\*.xlsx"        'change folder path and workbook filespec as required
    
    With ThisWorkbook.Worksheets(1)
        .Cells.Clear
        Set destCell = .Range("A1")
        r = 0
    End With
    
    folderPath = Left(fileSpec, InStrRev(fileSpec, "\"))
    
    fileName = Dir(fileSpec)
    Do While Len(fileName) <> 0
        Workbooks.Open folderPath & fileName
        ActiveWorkbook.Worksheets("Information").Rows("2:180").Copy destCell.Offset(r)
        ActiveWorkbook.Close saveChanges:=False
        r = r + 180 - 2 + 1
        fileName = Dir
    Loop
    
    MsgBox "Finished"
    
End Sub
 
Last edited:
Upvote 0
Many Thanks John, it was really helpful. I am facing a problem, the macro is copying formulas rather than values, could you please improve the macro for pasting the 'values' only. (paste values only).
___Thanks!





Try this macro in your Master workbook,changing the workbook filespec where shown. The rows are copied to the first sheet in the Master workbook.
Code:
Public Sub Copy_Rows_From_Workbooks2()

    Dim fileSpec As String, folderPath As String, fileName As String
    Dim destCell As Range, r As Long
    
    fileSpec = "C:\Data\*.xlsx"        'change folder path and workbook filespec as required
    
    With ThisWorkbook.Worksheets(1)
        .Cells.Clear
        Set destCell = .Range("A1")
        r = 0
    End With
    
    folderPath = Left(fileSpec, InStrRev(fileSpec, "\"))
    
    fileName = Dir(fileSpec)
    Do While Len(fileName) <> 0
        Workbooks.Open folderPath & fileName
        ActiveWorkbook.Worksheets("Information").Rows("2:180").Copy destCell.Offset(r)
        ActiveWorkbook.Close saveChanges:=False
        r = r + 180 - 2 + 1
        fileName = Dir
    Loop
    
    MsgBox "Finished"
    
End Sub
 
Upvote 0
Replace the Copy line with:
Code:
        ActiveWorkbook.Worksheets(1).Rows("2:180").Copy
        destCell.Offset(r).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
If you know which source columns are needed you can do it in a single line (destRange.Value = sourceRange.Value) avoiding the above copy/paste via the clipboard.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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
Back
Top