Excel Macro Help

yaroslav89

New Member
Joined
Jul 11, 2018
Messages
10
Hello everyone,
Just quickly, I am trying to create a macro where it will copy specific range in various workbooks and paste in a new workbook. My problem is that when I run a macro not all workbooks available at the time of running a macro. So Microsoft gives an error message "file either moved or deleted." Is there a way to go about if workbook is not found move to the next workbook. Below code works fine if all workbooks are in the specified path, but in my experience this is not the case. Some will be missing. In advance any help much appreciated!

Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

Set x = Workbooks.Open("C:\Users\Test 1", Password:="fun")
Set y = Workbooks.Open("C:\Users\Result 1", Password:="fun2")

Set ws1 = x.Sheets("Sheet1")
Set ws2 = y.Sheets("Sheet1")

ws1.Cells.Range("A1:R6").Copy ws2.Range("A1")

y.Close True
x.Close False

Set x = Workbooks.Open("C:\Users\Test 2", Password:="fun")
Set y = Workbooks.Open("C:\Users\Result 1", Password:="fun2")

Set ws1 = x.Sheets("Sheet1")
Set ws2 = y.Sheets("Sheet1")

ws1.Cells.Range("A1:R6").Copy ws2.Range("A7")

y.Close True
x.Close False
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
After the second Dim line add this line:

On Error Resume Next
 
Upvote 0
Insert 2 new lines currently Before the current Error-Line

and enter:

On Error
Resume Next
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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