Micro that checks if file exists

P

pete

Hi, everyone.
I have a micro that opens other files to copy data to this file. How can I
make it to skip a file that does not exist and continue to the next one?

Thanks for your help.
 
B

Bob Phillips

Set thisFile = Workbooks.Open("filename")
If Not thisFile Is Nothing Then
'your code
Else
'get the next
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
P

pete

Bob,

Thanks for your clue. I still have a problem; if the file does not exist, I
have the "run-time error 1004" which terminates the macro because it will not
allow me to continue.

Anything else I need to add so I if it cannot find the file, it will go to
the next one?

Thanks,
Pete
 
B

Bob Phillips

Sorry, it shoule be error wrapped

On Error Resume Next
Set thisFile = Workbooks.Open("filename")
On Error Goto 0
If Not thisFile Is Nothing Then
'your code
Else
'get the next
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
P

pete

Thanks again, Bob, for your timely fix. I almost got it, except the "Go to 0".
What can I substitute for "0"?
 
M

Myrna Larson

If you are talking about On Error Goto 0, that statement turns error trapping
off. If you mean a standard Goto statement (yes, it's still around), you have
to set up line numbers/labels and use those in your Goto statements.
 
V

veljo

Hi Pete,
Could you make public all the code. Seems it is what I am looking for for a
long time.
Thanks,
veljo


"pete" kirjutas:
 
P

pete

Hi Veljo,

I used Bob's suggestion,
1.
On Errror Resume Next
Set nextFile = Workbooks.Open("filename")
On error Goto 0

This portion will get rid of the irritation "run-time error".

2.
If Not nextFile Is Nothing Then
...... begin your code
Else
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top