Copying to Multiple Sheets in folder from one open sheet

gunjan8882

New Member
Joined
Nov 10, 2010
Messages
19
Dear all.

I am getting error for the following macro for copying and pasting data to multiple sheets in the folder. It seems after saving the file, clipboard gets clear and the copy text from the file gets unselected. Hence, the copy/paste function works for the first file and gives error for the subsequent files. Kindly help to correct the code. Thanks in advance.

-----------------------------------------------------------------------------------------
Private Sub CommandButton1_Click()Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = oFSO.GetFolder("J:\Flare Scenarios")
Workbooks("Page No.xlsm").Activate
ActiveSheet.Range("R2:V7").Select
Application.CutCopyMode = True
Selection.Copy
For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path
ActiveSheet.Range("C155").Select
ActiveSheet.Paste
Sheets("Module (LT)").Select
ActiveSheet.Range("C155").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Set oFSO = Nothing
End Sub
---------------------------------------------------------------------------------------------------------------------
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi there.

Amend your code to move the CutCopyMode = False line to after the Next file line so you will then have:
Code:
Private Sub CommandButton1_Click()Dim oFSO
Dim Folder As Object
Dim Files As Object
Dim file As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = oFSO.GetFolder("J:\Flare Scenarios")
Workbooks("Page No.xlsm").Activate
ActiveSheet.Range("R2:V7").Select
Application.CutCopyMode = True
Selection.Copy
For Each file In Folder.Files
If file.Type Like "*Microsoft Excel*" Then
Workbooks.Open Filename:=file.Path
ActiveSheet.Range("C155").Select
ActiveSheet.Paste
Sheets("Module (LT)").Select
ActiveSheet.Range("C155").Select
ActiveSheet.Paste
ActiveWorkbook.Close SaveChanges:=True
End If
Next file
Application.CutCopyMode = False
Set oFSO = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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