Checking if Files Exist

Balfin

Board Regular
Joined
Dec 29, 2005
Messages
119
Hi,

Is there a way to check to see if there are any .xls files, open or closed, in a given directory?

Thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about this?

Code:
Sub FindXLS()

    Dim FS As FileSearch
    Dim FilePath As String, FileSpec As String

    FilePath = "C:\Test\" '<----- Edit as needed
    FileSpec = "*.xls"

    Set FS = Application.FileSearch
    With FS
        .LookIn = FilePath
        .FileName = FileSpec
        .Execute
        If .FoundFiles.Count = 0 Then
            MsgBox "No Files Found"
            Exit Sub
        Else
            'Your code here
        End If
    End With
End Sub
 
Upvote 0
If in 2007 or above, you won't be able to make use of FileSearch, but you could use Dir:

Code:
Sub Test()
Dim strPath as String, strFilter as String, strResult as String
strPath = "C:\"   'amend as appropriate
strFilter = "*.xls"   'amend as appropriate
strResult = strPath & "\" & strFilter
If strResult<>"" Then
   'xls file found!!!   COde as appropriate
End If
End Sub
 
Upvote 0
Thanks! That worked great!

What does ".Execute" do? It was the only line I was missing and I needed it in there to work.
 
Upvote 0
FileSearch doesn't return any results until you use the execute command - you can think of it as akin to having the car ready to go but not having turned the key in the ignition so the car doesn't go anywhere.
 
Upvote 0
Richard

Is your code perhaps missing a little something?

Like Dir?:)
 
Upvote 0
Richard

Is your code perhaps missing a little something?

Like Dir?:)

Ah you mean the Dir() in invisible ink??? :oops:

Code:
Sub Test()
Dim strPath as String, strFilter as String, strResult as String
strPath = "C:\"   'amend as appropriate
strFilter = "*.xls"   'amend as appropriate
strResult = Dir(strPath & "\" & strFilter)
If strResult<>"" Then
   'xls file found!!!   COde as appropriate
End If
End Sub

Thank you Norie :biggrin:
 
Upvote 0
So the Filesearch method is no more? That stinks. I guess I will need to get more familiar with DIR. I wonder what the reasoning was on that decision.
 
Upvote 0
So the Filesearch method is no more? That stinks. I guess I will need to get more familiar with DIR. I wonder what the reasoning was on that decision.

Yep - I've heard rumours that FileSearch is buggy but then I've never actually seen anyone back that up with evidence. I used to use it all the time in my code (at work they are still on 2003 and hopefully won't upgrade for a while) and don't relish the prospect of modifying all my existing code...
 
Upvote 0
Richard

I don't have any actual evidence but I'm sure in the past I've experienced difficulty with FileSearch.

Admittedly though it was normally my problem.

But I'm pretty sure a few times it was VBA/Excel causing the problem, honest.:)
 
Upvote 0

Forum statistics

Threads
1,214,524
Messages
6,120,049
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top