VBA On Error and opening files

Gerald Higgins

Well-known Member
Joined
Mar 26, 2007
Messages
9,258
Hi, I'm still trying to teach myself VBA, thanks in advance for any help.

I have an application where I need to open a number of files in sequence, copy data out of them, and paste that data into the main file.
Some of those files may not exist, and if that happens I want to store the file name in a text string which I can then present at the end to say "These files failed".

I hope to end up doing this as a loop, but before I get there, I'm just trying to make it work for a single file, and I'm having trouble with the error handling.

The code is essentially this (I've slimmed it down slightly to simplify for posting purposes)

Code:
On Error GoTo FileOpenError
    Workbooks.Open Filename:=SourceFile
    
Range("B16:U115").Copy

    Windows("[Filename].xlsm").Activate

    Sheets("Data1").Activate
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Windows(2).Activate
    ActiveWorkbook.Close

GoTo FileOpenErrorBypass:

FileOpenError:
   FileError = FileError & SourceFile & ". "
   MsgBox ("File Error " & FileError)
Resume NextFile

FileOpenErrorBypass:
   Resume NextFile
NextFile:

What this is MEANT to do is -
- open another file (the file name is held in SourceFile)
- copy data in that other file
- paste that data into the main file
- close the other file without saving it
- move on to the next file, which I'll do later by adding a loop
- IF the source file does not exist, then go into the error handling part by storing the file name in FileError, and display a message box saying that.
- IF the source file DOES exist, the error handling part should be bypassed.

When I test this on a single file that does exist, all of this works, EXCEPT it enters the Error Handling part at the end, displaying the message box with the file name as if it did not exist, but it does exist and the data has been copied out of it.

What am I doing wrong ?
I'm guessing my OnError and Resume statements are in the wrong place, or maybe I'm doing something even more stupid.
Any ideas ?
Thanks again for any assistance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I'd do it like
Code:
   Dim Wbk As Workbook
   
   Set Wbk = Nothing
   On Error Resume Next
   Set Wbk = Workbooks.Open(Filename:=SourceFile)
   On Error GoTo 0
   
   If Not Wbk Is Nothing Then
      Range("B16:U115").Copy

    Windows("[Filename].xlsm").Activate

    Sheets("Data1").Activate
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

      Wbk.Close False
   Else
      FileError = FileError & SourceFile & ". "
      MsgBox ("File Error " & FileError)
   End If
 
Upvote 0
I'd use Dir to test for the file before trying to open it.
 
Upvote 0
Fluff - thank you ! I'll have a go at that.
RoryA - thank you too ! How exactly would you " . . . use Dir to test for the file . . . " ?
 
Upvote 0
Something like:

Code:
   If Dir(Sourcefile) <> vbnullstring then
dim wb as workbook
set wb = Workbooks.Open(Filename:=SourceFile)
Range("B16:U115").Copy

    Windows("[Filename].xlsm").Activate

    Sheets("Data1").Activate
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

      wb.Close False

Else
   FileError = FileError & SourceFile & ". "
end if
 
Upvote 0
Thanks again guys, much appreciated !

Fluff - as far as I could tell there was no instruction to actually open the target file in your suggestion, so I got it to work like this
Rich (BB code):
   On Error Resume Next

   Workbooks.Open Filename:=SourceFile

   Set Wbk = Workbooks.Open(Filename:=SourceFile)

This seems to work correctly for both cases, i.e. the target file existing, and the target file not existing.
If it exists, the file is opened, data is copied out, file is closed, and it moves on to the next file.
If it does not exist, the error handling section is activated, and then it moves on to the next file.
All good, thanks very much !

RoryA - I have not tried your's yet, but I aim to, thanks again for your suggestion.
Do you think it is materially different from Fluff's ?
I don't know enough about VBA to recognise this myself.
 
Upvote 0
The workbook is opened by the line of code directly under the line you added in red. I would recommend removing that line in red as it could very easily cause problems.

Rory's suggestion is a lot better as it checks to see if the file exists before trying to open it.
 
Upvote 0
Fluff, thanks, my mistake sorry !
I didn't appreciate that this line
Code:
   Set Wbk = Workbooks.Open(Filename:=SourceFile)
was actually opening the file.
I mean I thought it might be, but I was getting an error condition, but it turned out this was because the target file has worksheet protection on it, and I had forgotten to insert the line of code to turn it off

So this variation works as well . . .
Code:
   Set Wbk = Workbooks.Open(Filename:=SourceFile)
ActiveSheet.Unprotect [PASSWORD]

I'll try RoryA's solution as well, but in the meantime, I think I have at least one workable solution.

Thanks again !
 
Upvote 0
Glad it's working & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,779
Members
449,123
Latest member
StorageQueen24

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