Please help on error opening files vba

roberttaekim

New Member
Joined
Jan 22, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi everyone. Hoping someone can help. I have this code which opens a file on the network and copy and pastes the content to a sheet and then opens the next file and copy and pastes to another sheet. How do i put some kind of on error resume so that if the first file cannot open because it is not available it just keeps going on to the next part of the code to open the next file and copy paste?

Really appreciate the help

VBA Code:
Dim NmStr As String
NmStr = ActiveWorkbook.Name
Windows(NmStr).Activate

Report = Sheets("Documentation").Range("C5").Value
Workbooks.Open Filename:=Report
ActiveSheet.Cells.Copy
Windows(NmStr).Activate
Sheets("BS").Select
Range("A1").Select
ActiveSheet.Paste

Report = Sheets("Documentation").Range("C6").Value
Workbooks.Open Filename:=Report
ActiveSheet.Cells.Copy
Windows(NmStr).Activate
Sheets("IS").Select
Range("A1").Select
ActiveSheet.Paste
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Use on Error Resume Next in the beginning of code.

VBA Code:
On Error Resume Next
Dim NmStr As String
NmStr = ActiveWorkbook.Name
Windows(NmStr).Activate

Report = Sheets("Documentation").Range("C5").Value
Workbooks.Open Filename:=Report
ActiveSheet.Cells.Copy
Windows(NmStr).Activate
Sheets("BS").Select
Range("A1").Select
ActiveSheet.Paste

Report = Sheets("Documentation").Range("C6").Value
Workbooks.Open Filename:=Report
ActiveSheet.Cells.Copy
Windows(NmStr).Activate
Sheets("IS").Select
Range("A1").Select
ActiveSheet.Paste
 
Upvote 0
If the file is not there i get an error saying cannot find file. I would like for it jus to skip that code and then move to the next code that opens the file and copy/pastes it. Can that be done?
 
Upvote 0
1. Use On Error resume next before code where you are opening the file.

2. If required you can check whether file exists or not using below code. use file name with path inside Dir function.

VBA Code:
If Dir("path\filename.xlsm") = vbNullString Then
        MsgBox "File not exists"
else
     'Use your code
    End If

3. Share the code where you are opening the file, error you are getting and on which line of code.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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