Dir() Question

A

AirgasRob

I am trying to run code to detect if 4 files exist before I try to import them.

I got the first file check to work fine but I am unsure how to have it check
for all 4 files before running the rest of the code.


If Dir("C:\TransferStation\FILE1.txt") = "" Then
MsgBox ("File doesn't exist")
Else
'Run the rest of the sub
 
W

Wolfgang Kais

Hello AirgasRob.

AirgasRob said:
I am trying to run code to detect if 4 files exist before I try to
import them.

I got the first file check to work fine but I am unsure how to have
it check for all 4 files before running the rest of the code.

If Dir("C:\TransferStation\FILE1.txt") = "" Then
MsgBox ("File doesn't exist")
Else
'Run the rest of the sub

Maybe like this:

If Dir("C:\TransferStation\FILE1.txt") = "" Then
MsgBox ("File1 doesn't exist", vbExclamation, "Import")
ElseIf Dir("C:\TransferStation\FILE2.txt") = "" Then
MsgBox ("File2 doesn't exist", vbExclamation, "Import")
ElseIf Dir("C:\TransferStation\FILE3.txt") = "" Then
MsgBox ("File3 doesn't exist", vbExclamation, "Import")
ElseIf Dir("C:\TransferStation\FILE4.txt") = "" Then
MsgBox ("File4 doesn't exist", vbExclamation, "Import")
Else
'Run the rest of the sub
End If

Or like this:
Select Case ""
Case Dir("C:\TransferStation\FILE1.txt")
MsgBox ("File1 doesn't exist", vbExclamation, "Import")
Case Dir("C:\TransferStation\FILE2.txt")
MsgBox ("File2 doesn't exist", vbExclamation, "Import")
Case Dir("C:\TransferStation\FILE3.txt")
MsgBox ("File3 doesn't exist", vbExclamation, "Import")
Case Dir("C:\TransferStation\FILE4.txt")
MsgBox ("File4 doesn't exist", vbExclamation, "Import")
Case Else
'Run the rest of the sub
End Select
 
K

Klatuu

Look in VBA Help for the Dir() function. The example shows exactly how to do
that. You could just copy/paste the example into your code and change the
names to suit your situation.
 
A

AirgasRob

Thank you both!

Klatuu said:
Look in VBA Help for the Dir() function. The example shows exactly how to do
that. You could just copy/paste the example into your code and change the
names to suit your situation.
 
Top