Please help on error opening files vba

roberttaekim

New Member
Joined
Jan 22, 2021
Messages
2
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
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

roberttaekim

New Member
Joined
Jan 22, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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?
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,832
Messages
5,627,152
Members
416,224
Latest member
RichardHell

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
Top