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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Reid

Well-known Member
Joined
Oct 29, 2004
Messages
593
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
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

Balfin

Board Regular
Joined
Dec 29, 2005
Messages
119
Thanks! That worked great!

What does ".Execute" do? It was the only line I was missing and I needed it in there to work.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Richard

Is your code perhaps missing a little something?

Like Dir?:)
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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:
 

Reid

Well-known Member
Joined
Oct 29, 2004
Messages
593
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.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
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.:)
 

Forum statistics

Threads
1,181,055
Messages
5,927,861
Members
436,573
Latest member
CMR237

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
Top