Opening File (error handeling)

  • Thread starter Thread starter Stephen
  • Start date Start date
S

Stephen

Hi all,

I need to open 2 excel files (example; "A.xls" and "B.xls") with
macro but it can happen that A.xls or B.xls is not present. Is there
way that if A.xls is not present it continues with opening B.xls or th
other way around?

Stephe
 
Hi Stephen

try something like
if dir("C:\temp\a.xls") = "" then
' file doesn't exist
Else
workbooks.Open "C:\temp\a.xls"
' process file
End if
 
Stephen,

You can check if the file exists and not try to open it if not, by something
like this:

wrkbook = "PathAndFileName.xls"
If Dir(wrkbook) <> "" Then Workbooks.Open wrkbook

If you need to do this for, say, three or more, then it's preferable to
either assign the individual workbook names to an array, or keep then on a
worksheet, and loop through.

HTH,
Nikos
 

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

Back
Top