Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I think the forum is a great idea, especially for those of us in consulting engineering. Keep up the good work!..."

Geography

Where in the world do Tek-Tips members come from?
montypython1 (TechnicalUser)
20 May 12 4:39
Greetings,

I have an Excel macro that calls certain files in order to extract data. Since I will be distributing this to different departments, there is a good chance that from time to time, some of these files will not exist (since these data files are created through input by people within that department).

How can I test to see if the file exists, before trying to retrieve it?

Thanks,
Dave
macropod (TechnicalUser)
20 May 12 6:25
If Dir 'Filepath & name' <> "" Then
'Exists
End if

Cheers
Paul Edstein
[MS MVP - Word]

montypython1 (TechnicalUser)
21 May 12 2:49
Hi Paul,

Thanks for the suggestion, but it doesn't seem to work as expected, but maybe I'm missing something. Here is my code:

If lcFolder & "\ED01_FI_Used03_m0.CSV" <> "" Then
Sheets("01Used_m0").Select
Range("A22:BC28").Select
Selection.ClearContents
Workbooks.Open Filename:= _
lcFolder & "\ED01_FI_Used03_m0.CSV"
'error occurs on line above
Range("A1:BC2").Select
Selection.Copy
'then do stuff with the data
End If

Even though the file does NOT exist, it still tries to run the code inside the IF/ENDIF statement. Note: the variable "lnFolder" refers to "C:\users\public\documents" and it DOES exist, but the file DOES NOT exist (I deleted it for testing purposes).

Any ideas why it would still run the code when the IF clause is NOT met?
Also, How do you post code in Tek-Tips so it shows the indents?

Thanks,
Dave
macropod (TechnicalUser)
21 May 12 2:56
Hi Dave,

That's hardly surprising - you have no Dir test! Try:

CODE

If Dir(lcFolder & "\ED01_FI_Used03_m0.CSV") <> "" Then Sheets("01Used_m0").Range("A22:BC28").ClearContents Workbooks.Open Filename:=lcFolder & "\ED01_FI_Used03_m0.CSV" 'error occurs on line above Range("A1:BC2").Copy 'then do stuff with the data End If

Cheers
Paul Edstein
[MS MVP - Word]

montypython1 (TechnicalUser)
21 May 12 3:02
Ahhhhhh ..... You are absolutely correct.
I overlooked the DIR keyword.

I just tried your suggestion and it works perfectly.

Thanks,
Dave
Andrzejek (Programmer)
21 May 12 8:23

And as far as "How do you post code in Tek-Tips so it shows the indents?", if you type:

[code]

Some code goes here
other code
Last line
[/code]


You get:

CODE

Some code goes here other code Last line

Have fun.

---- Andy

montypython1 (TechnicalUser)
21 May 12 15:56
Hi Andy,

Thanks ... That is exactly what I needed.
I figured it was something easy ... I just couldn't find it.

Dave

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close