Copying multiple workbooks to multiple workbooks

dtmssmile

New Member
Joined
Jan 5, 2017
Messages
6
I have hundreds of workbook that contain data that have to be pasted onto respective workbooks and I wrote 4 modules.
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = "C:\Users\HKIEd\Downloads" 'change to suit

If Right(folderPath, 1) <> "" Then folderPath = folderPath + ""

filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)

'Call a subroutine here to operate on the just-opened workbook
Call Copy


filename = Dir
Loop
Application.ScreenUpdating = True
End Sub

Sub Copy()
'
' Macro1 Macro
'


'
Sheets(1).Select
Range("B2:B81").Select
Range("B81").Activate
Selection.Copy
Call Pastesrc

End Sub

Sub Pastesrc()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = "C:\Users\HKIEd\Documents" 'change to suit

If Right(folderPath, 1) <> "" Then folderPath = folderPath + ""

filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)

'Call a subroutine here to operate on the just-opened workbook
Call Paste


filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
Sub Paste()


Sheets(1).Select
ActiveSheet.Unprotect "abc"
Range("A4").Activate
Range("A4").Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Protect "abc"

End Sub

However, it gets stuck at ActiveSheet.PasteSpecial Paste:=xlPasteValues. I am new to VBA, not sure what happened and not sure if I got the scripts right. Pls help!!
Actually I would like the name of the source to match with that of the destination when the data is copied and pasted but I have no idea how to do so.Pls help!!!!
Thanks a great great deal
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
[FONT=&quot]I have hundreds of workbook that contain data that have to be pasted onto respective workbooks and I wrote 4 modules.[/FONT]
<code style="font-family: "Courier New", courier, monospace; margin: 0px 2px; padding: 15px; border: 0px; background-color: transparent; border-radius: 2px; word-break: normal; display: block; font-size: 1em; line-height: 16px; overflow: auto;">Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = "C:\Users\HKIEd\Downloads" 'change to suit

If Right(folderPath, 1) <> "" Then folderPath = folderPath + ""

filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)

'Call a subroutine here to operate on the just-opened workbook
Call Copy


filename = Dir
Loop
Application.ScreenUpdating = True
End Sub

Sub Copy()

Sheets(1).Select
Range("B2:B81").Select
Range("B81").Activate
Selection.Copy
Call Pastesrc

End Sub

Sub Pastesrc()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = "C:\Users\HKIEd\Documents" 'change to suit

If Right(folderPath, 1) <> "" Then folderPath = folderPath + ""

filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)

'Call a subroutine here to operate on the just-opened workbook
Call Paste


filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
Sub Paste()


Sheets(1).Select
ActiveSheet.Unprotect "abc"
Range("A4").Activate
Range("A4").Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Protect "abc"

End Sub
</code>[FONT=&quot]However, it gets stuck at ActiveSheet.PasteSpecial Paste:=xlPasteValues. I am new to VBA, not sure what happened and not sure if I got the scripts right. Pls help!! Actually I would like the name of the source to match with that of the destination when the data is copied and pasted but I have no idea how to do so.Pls help!!!! Thanks a great great deal[/FONT]
[FONT=&quot]And I tried replace the macro Paste with the following script but still getting runtime error 1004: Paste method range class failed!_![/FONT]
<code style="font-family: "Courier New", courier, monospace; margin: 0px 2px; padding: 15px; border: 0px; background-color: transparent; border-radius: 2px; word-break: normal; display: block; font-size: 1em; line-height: 16px; overflow: auto;"> Sub Paste()

With Sheets(1)
ActiveSheet.Unprotect "abc"
Range("A4").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Protect "abc"
Application.CutCopyMode = False
End With

End Sub</code>
 
Upvote 0
And I tried replace the macro Paste with the following script but still getting runtime error 1004: Paste method range class failed!_!
<code style="margin: 0px 2px; padding: 15px; font-family: "Courier New", courier, monospace; line-height: 16px; color: rgb(51, 51, 51); font-size: 1em; background-color: transparent; border: 0px; border-radius: 2px; word-break: normal; display: block; overflow: auto;">Sub Paste()

With Sheets(1)
ActiveSheet.Unprotect "abc"
Range("A4").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Protect "abc"
Application.CutCopyMode = False
End With

End Sub</code>
 
Upvote 0
ActiveSheet.Unprotect "abc"
ActiveSheet.Unprotect Password:="abc"

works?
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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