Check if file exists without opening it

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
I have a code that opens and closes a workbook to check if it exists. The problem is that opening that workbook takes quite a while since it's size is over 10MB. I'm pretty sure we have a smarter way to do this, for example some loop to go thru filenames in directory and saying that "yep, it's there" or "nope, not there".

So something like
Code:
Sub CheckForFile()
    Dim strFile As string
    Dim strFolder As string
    Dim bFound As Boolean

    bFound = FileExists(strFile, strFolder)

    If bFound Then
        Debug.Print "yep, it's there"
    Else
        Debug.Print "nope, not there"
    End If
End Sub

Function FileExists(strFile As String, strFolder As String)
    Dim FileName As File 'What's the correct objects here?
    Dim Folder As FileFolder 'What's the correct objects here?
    FileName = file(strFile) 'How does this one go?
    Folder = folder(strFile) 'How does this one go?
    
    For Each FileName In Folder 
        If FileName.Name = strFile Then
            FileExists = True
            Exit Function
        End If
    Next File
    FileExists = False
End Function

I would imagine that for someone handling files with VBA often this is a piece of cake.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try

Code:
Function FileExists(strFile As String, strFolder As String) As Boolean
FileExists = Dir(strFolder & "\" & strFile) <> ""
End Function
 
Upvote 0
Try

Code:
Function FileExists(strFile As String, strFolder As String) As Boolean
FileExists = Dir(strFolder & "\" & strFile) <> ""
End Function

Kind of depressing when someone pulls it off with one line of code. :laugh: But thanks anyway, that's pretty much exactly what I was looking for!
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

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