Macro for copying cells from multiple workbooks

Moeen

New Member
Joined
Aug 23, 2013
Messages
2
I am new to this forum and I would be very thankful if anyone can help me out.
I need a code that will copy certain cells from all sheets of all files in a folder and then list/paste them down in another master/summary sheet.
All workbooks in the folder have multiple sheets and in each sheet the cells which I want to copy are A2. B2 and C2
Example :
In work -Book1
Sheet 1
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]47401[/TD]
[TD]ABC-1[/TD]
[TD]12345678[/TD]
[/TR]
</tbody>[/TABLE]

Sheet 2
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]47402[/TD]
[TD]ABC-2[/TD]
[TD]51354351[/TD]
[/TR]
</tbody>[/TABLE]


Sheet 3
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]47403[/TD]
[TD]ABC-3[/TD]
[TD]54542545[/TD]
[/TR]
</tbody>[/TABLE]


In work Book-2
Sheet 1
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]37601[/TD]
[TD]xyz-1[/TD]
[TD]12345678[/TD]
[/TR]
</tbody>[/TABLE]

Sheet 2
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]37602[/TD]
[TD]xyz-2[/TD]
[TD]51354351[/TD]
[/TR]
</tbody>[/TABLE]


Sheet 3
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]37603[/TD]
[TD]xyz-3[/TD]
[TD]54542545[/TD]
[/TR]
</tbody>[/TABLE]


And so on.
I want to compile the highlighted cells in the following format in the summary file.
[TABLE="width: 427, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]CODE[/TD]
[TD]PAGE[/TD]
[TD]BALANCE[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]47401[/TD]
[TD]ABC-1[/TD]
[TD]12,345,678[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]47402[/TD]
[TD]ABC-2[/TD]
[TD]51,354,351[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]47403[/TD]
[TD]ABC-3[/TD]
[TD]54,542,545[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]37601[/TD]
[TD]xyz-1[/TD]
[TD]12,345,678[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]37601[/TD]
[TD]xyz-2[/TD]
[TD]51,354,351[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]37603[/TD]
[TD]xyz-3[/TD]
[TD]54,542,545[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]





Any help would be appreciated. I have searched the internet for this and have found the following links but I wasn’t able to make them work.
http://www.mrexcel.com/forum/excel-questions/673353-macro-copy-data-multiple-excel-files.html
http://www.mrexcel.com/forum/excel-questions/508186-loop-through-folder-run-macro-all-workbooks.html
The following code does what I want for 1 workbook only
Sub Button10_Click()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Select
'Sub CopyData()
With ActiveSheet

.Range("A2").Select
Selection.Copy
Sheets("Proof Sheet").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
.Range("B2").Select
Selection.Copy
Sheets("Proof Sheet").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
.Range("C2").Select
Selection.Copy
Sheets("Proof Sheet").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues

End With
Application.ScreenUpdating = False
Next
Application.ScreenUpdating = False
Sheets("Proof Sheet").Select
End Sub

I can provide futher details if needed

thanks in advance :)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
this should do

Code:
Sub MergeAllWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim FileName As String
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    
    ' Set summarysheet to activeworkbook/activesheet where the macro runs
    Set SummarySheet = ActiveWorkbook.ActiveSheet
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Temp\exceltest\"
    
    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")
    
    ' Loop until Dir returns an empty string.
    Do While FileName <> ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)

        'loop through all Sheets in WorkBk
        For Each sh In WorkBk.Worksheets
          
        ' Set the source range to be A9 through C9.
          Set SourceRange = Sheets(sh.Name).Range("A2:C2")
        
        ' Set the destination range to start at column B and
        ' be the same size as the source range.
        Set DestRange = SummarySheet.Range("A" & SummarySheet.Range("A" & Rows.Count).End(xlUp).Row + 1)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
           SourceRange.Columns.Count)
        
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
        
        Next sh
        
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
        
        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop
    
    ' Call AutoFit on the destination sheet so that all
    ' data is readable.
    ActiveSheet.Columns.AutoFit
End Sub
 
Upvote 0
HI I have a similar issue.
I am wanting to copy cells/columns from 1 workbook to another using similar principles and on a weekly basis. However the workbook I am copying data from will have a different name each week. Is there anything I could put to say use workbooks that are open or something else?

Thanks in advance!
 
Upvote 0
HI I have a similar issue.
I am wanting to copy cells/columns from 1 workbook to another using similar principles and on a weekly basis. However the workbook I am copying data from will have a different name each week. Is there anything I could put to say use workbooks that are open or something else?

Thanks in advance!

you should open your own thread here an example to loop through all workbook that are open

Code:
Sub WBLoop()
    Dim wbk As Workbook, rngToCopy As Range, rngToPaste As Range
     
    With Worksheets("Sheet1")
        For Each wbk In Workbooks
            'loop through the Open workbooks
            If wbk.Name <> ThisWorkbook.Name Then
                'exclude this workbook from the Loop
                Set rngToPaste = .Range("A65536").End(xlUp).Offset(1, 0)
                'set the target For the paste
                Set rngToCopy = wbk.Range("A1:H10")
                'set the range To be copied
                rngToCopy.Copy Destination:=rngToPaste
                'do the copying
            End If
        Next
    End With
     
End Sub
 
Upvote 0
I cannot thankyou enough. bless your soul.

Is there anyway to modify the code so that it, instead of pasting the values, it pastes links to the original values. like the pastespecial > paste as links.

I tried to do it myself but i failed
this is what I did, i replaced your ;

"DestRange.Value = SourceRange.Value"

with this :

SourceRange.Select
Selection.Copy
Application.Goto DestRange.Select
ActiveSheet.Paste Link:=True



and some other similar modifications, all gave errors.

a big thanks in advance for any help you will give.
 
Upvote 0
this should do

Code:
Sub MergeAllWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim FileName As String
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    
    ' Set summarysheet to activeworkbook/activesheet where the macro runs
    Set SummarySheet = ActiveWorkbook.ActiveSheet
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Temp\exceltest\"
    
    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")
    
    ' Loop until Dir returns an empty string.
    Do While FileName <> ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)

        'loop through all Sheets in WorkBk
        For Each sh In WorkBk.Worksheets
          
        ' Set the source range to be A9 through C9.
          Set SourceRange = Sheets(sh.Name).Range("A2:C2")
        
        ' Set the destination range to start at column B and
        ' be the same size as the source range.
        Set DestRange = SummarySheet.Range("A" & SummarySheet.Range("A" & Rows.Count).End(xlUp).Row + 1)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
           SourceRange.Columns.Count)
        
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
        
        Next sh
        
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
        
        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop
    
    ' Call AutoFit on the destination sheet so that all
    ' data is readable.
    ActiveSheet.Columns.AutoFit
End Sub

I'm using this code as a template and it's nearly working. I need it to copy cells (D3,D4,D5,D6,D7,D8,K7,R3,A19:T26) from each sheet and put them into the main workbook moving across until it moves to the next sheet and then start a new row until it has worked through all the files. It's currently putting everything down the first column and I'm getting a subscript error when I try to put this range in.

Thanks for any help I can get, it seems like I'm really close, but I'm very new to VBA so it's taking forever to troubleshoot something that is probably very easy for a lot of you.
 
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