Paste special from multiple workbooks in a folder to a master workbook

Hankthetanker

New Member
Joined
May 12, 2016
Messages
4
Hi Excel PROs

I need some help. I'm trying to paste values from all files in a folder to a master folder but i keep getting errors in the paste process. This is my code:

Sub LoopThrough()Dim MyFile As String
Dim erow
Dim FilePath As String
Dim DestWB As Workbook
Dim SourceWB As Workbook


'this way we know the one where the code is running and the destination for our copies
Set DestWB = ThisWorkbook


FilePath = "C:\data\"
MyFile = Dir(FilePath)


Do While Len(MyFile) > 0
If MyFile = "Master.xlsm" Then
Exit Sub
End If


Set SourceWB = Workbooks.Open(FilePath & MyFile)
Workbooks.Open (FilePath & MyFile)
Range("A1:L51").Copy
'Move the close to AFTER you do the paste
'NOTE: You may have to make a change here:
DestWB.Range(Cells(erow, 1), Cells(erow, 12)).PasteSpecial xlValues
SourceWB.Close False
MyFile = Dir
Loop


End Sub

Can i please get some help?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,
welcome to the board.

Not tested but see if this update to your code does what you want.

Rich (BB code):
Sub LoopThrough()
    Dim MyFile As String
    Dim FilePath As String
    Dim DestWS As Worksheet
    Dim SourceWB As Workbook
    
    
    'Master worksheet - change name as required
    Set DestWS = ThisWorkbook.Worksheets("Sheet1")
    
    
    FilePath = "C:\data\"
    MyFile = Dir(FilePath)
    
    Application.ScreenUpdating = False
    On Error GoTo myerror
    Do While Len(MyFile) > 0
    If MyFile <> "Master.xlsm" Then


    Set SourceWB = Workbooks.Open(FilePath & MyFile)
    
        SourceWB.Sheets(1).Range("A1:L51").Copy
        
        DestWS.Range("A" & DestWS.Cells(DestWS.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlValues
        
        Application.CutCopyMode = False
        
        SourceWB.Close False
    
    MyFile = Dir
    End If
    Set SourceWB = Nothing
    Loop
    
myerror:
Application.ScreenUpdating = True
If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

You will need to change the master sheet name shown in RED as required.

Hope Helpful

Dave
 
Upvote 0
Dave, Thank you so much for helping me. You have just saved me many hours every week from now. How would the world be without people like you. :)
 
Upvote 0
Dave, Thank you so much for helping me. You have just saved me many hours every week from now. How would the world be without people like you. :)

I don't always get things right going bit daft in my old age but glad adjustments helped you.

Many thanks for your kind feedback, very much appreciated.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

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