check for the existence of files using VBA

Dear Experts:

I am sure this can be done much, much better:

Sub Check4Files_exist()
If Dir("C:\myfolder\product_ab.xlsx") = "" Then
MsgBox ("file not found")
End If
If Dir("C:\myfolder\product_abc.xlsx") = "" Then
MsgBox ("file not found")
End If
If Dir("C:\myfolder\product_ab124.xlsx") = "" Then
MsgBox ("file not found")
End If
If Dir("C:\myfolder\product_1z4ab.xlsx") = "" Then
MsgBox ("file not found")
End If
End Sub

Open in new window


I would like the macro to check for the existence of these 4 excel files.

If all are found, the macro is to say so (such as "all files have been found") and a new macro called 'MyNewMacro' is to be called.

If one or any number of excel files (1 to 4) are missing in that directory, the macro is to say so, such as:

2 of the required 4 excel-files could not be found:
C:\myfolder\product_ab.xlsx
C:\myfolder\product_ab124.xlsx

In these cases the macro is to exit.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
Do you want someone to write the whole thing for you or are you looking to learn it yourself?

I would use the File System Object:

Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists("C:\myfolder\product_ab.xlsx") = True Then
   'File Exists
Else
   'File doesn't exist
End If

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Hi Lee,

thank you very much for your swift support.

To be honest with you, if someone could write the whole thing would be great.

Regards, Andreas
0
Rgonzo1971Commented:
Hi,

pls try
Sub Check4Files_exist()
strListm = ""
If Dir("C:\myfolder\product_ab.xlsx") = "" Then
strList = strList & "C:\myfolder\product_ab.xlsx" & vbCrLf
End If
If Dir("C:\myfolder\product_abc.xlsx") = "" Then
strList = strList & "C:\myfolder\product_abc.xlsx" & vbCrLf
End If
If Dir("C:\myfolder\product_ab124.xlsx") = "" Then
strList = strList & "C:\myfolder\product_ab124.xlsx" & vbCrLf
End If
If Dir("C:\myfolder\product_1z4ab.xlsx") = "" Then
strList = strList & "C:\myfolder\product_1z4ab.xlsx" & vbCrLf
End If
On Error Resume Next
aList = Split(strList, vbCrLf)
On Error GoTo 0
If UBound(aList) <> -1 Then '4-1
    MsgBox UBound(aList) & "of the required 4 excel-files could not be found:" & vbCrLf & strList
    'Exit Sub
Else
    Call MyNewMacro ' Macro
End If
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bill PrewCommented:
Here's a possible approach you could try that reduces the redundant code...

Sub Check4Files_exist()
    Dim MissingFiles As Boolean
    Dim RequiredFile As Variant

    MissingFiles = False

    For Each RequiredFile In Split("C:\myfolder\product_ab.xlsx,C:\myfolder\product_abc.xlsx,C:\myfolder\product_ab124.xlsx,C:\myfolder\product_1z4ab.xlsx", ",")
        If Dir(RequiredFile) = "" Then
            MissingFiles = True
            Exit For
        End If
    Next

    If MissingFiles Then
        MsgBox ("file not found")
    End If
End Sub

Open in new window


»bp
0
Andreas HermleTeam leaderAuthor Commented:
Dear both,

Rafael, your code is really great and comprehensive. Thank you very much for this great job.

Bill, thank you for your nice code. It is another approach which is interesting for me to get to know, although I am afraid to tell you that Rafael's code is exactly what I am looking for.

Thank you very much for your professional help and the time take. I really appreciate it.

Regards, Andreas
0
Bill PrewCommented:
Okay, thought you could handle the rest.  Just to wrap it up, here is the small changes to get to what you described.

Sub Check4Files_exist()
    Dim MissingCount As Integer
    Dim MissingList As String
    Dim RequiredFile As Variant

    MissingCount = 0
    MissingList = ""

    For Each RequiredFile In Split("C:\myfolder\product_ab.xlsx,C:\myfolder\product_abc.xlsx,C:\myfolder\product_ab124.xlsx,C:\myfolder\product_1z4ab.xlsx", ",")
        If Dir(RequiredFile) = "" Then
            MissingCount = MissingCount + 1
            MissingList = MissingList & vbCrLf & RequiredFile
        End If
    Next

    If MissingCount > 0 Then
        MsgBox MissingCOunt & " of the required 4 excel-files could not be found:" & MissingList
    Else
        Call MyNewMacro
    End If
End Sub

Open in new window


»bp
0
Andreas HermleTeam leaderAuthor Commented:
Oh, hi Bill, very nice, thank you very much for your great support. I really appreciate it. Works great :-)
0
Bill PrewCommented:
Very welcome.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.