Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Checking for the Existence of a File.

Checking for the Existence of a File

Written by Allen Wyatt (last updated May 8, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


2

John has a column of invoice numbers in a worksheet. He has a directory on the network where staff save a PDF of the actual invoice and name it using the same invoice number that is in the worksheet. Each invoice number in the worksheet should have a correspondingly named PDF in the directory on the network. John is looking for a way, within Excel, to check and verify that a PDF really does exist for each invoice number.

There is no way to do this using built-in Excel commands. You can, however, create a macro that will do the checking for you. For instance, consider the following simple user-defined function:

Function FileExists1(sPath As String)
    FileExists = Dir(sPath) <> ""
End Function

The routine simply returns a True or False value, based on whether the specified file exists. The value that is passed to the function needs to include a full path and file name. For example, if the file specification (including the path) were in cell A1, you could use the following in a cell:

=FileExists1(A1)

You may not, however, want to put the full path name into the cell. In that case, you could specify it in the actual formula, in this way:

=FileExists1("c:\your\path\here\" & A1 & ".pdf")

Of course, you could instead specify the path in the user-defined function:

Function FileExists2(sFile As String)
    sPath = "c:\your\path\here\" & sFile & ".pdf"
    FileExists = Dir(sPath) <> ""
End Function

With such a function you could easily create a formula in your worksheet that would "flag" any invoices missing from the directory:

=IF(FileExists2(A1),"","Missing Invoice")

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7512) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Checking for the Existence of a File.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Meaningless Text

Need to quickly put some text into a document, even if that text is essentially meaningless? Here's how to put this type ...

Discover More

Inserting Only Part of a File

You can easily insert one document within another document. What you may not know is that you can limit which part of a ...

Discover More

Word 2010 Styles and Templates (Table of Contents)

Styles are at the heart of Word's formatting power. Understanding how to use styles can greatly increase your ability to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Importing Huge Data Files

Sometimes, when importing data created by other programs, you may find that there is too much for Excel to handle. Here's ...

Discover More

Aligning Cells when Importing from CSV

When you import information from a CSV text file, Excel formats the data according to its default settings. Wouldn't it ...

Discover More

Closing Multiple Files

When working with multiple workbooks open, you may want a way to close them all with a single command. Here's the secret.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one minus 0?

2023-07-01 05:29:45

'Peter

@Nicky, You need a list of subfolders to scan that for each of the files. I have used a sub that works on the selected file names.

Sub CheckFilesExist()
Dim cc As Range, nn
Dim sStartingFolder As String, sFolder, sFile As String
Dim aSubFolders()
ReDim Preserve aSubFolders(0)

'Given the starting folder, look for subfolders and put them into an array.
'VBA forgets you asked for directories in subsequent passes using Dir(), hence Getattr()

sStartingFolder = Range("Startingfolder").Value ' (name ends with \)
sFolder = Dir(PathName:=sStartingFolder, Attributes:=vbDirectory)
Do While sFolder <> ""
If GetAttr(sStartingFolder & sFolder) = vbDirectory Then
If sFolder = "." Or sFolder = ".." Then ' skip system folders
ElseIf sFolder > "" Then
ReDim Preserve aSubFolders(UBound(aSubFolders) + 1)
aSubFolders(UBound(aSubFolders)) = sFolder
Else
Exit Do
End If
End If
sFolder = Dir()
Loop

'Then work through the selected file names of interest. Since this is a sub, I'm putting the True/False result into the cell to the right of the file name.

If UBound(aSubFolders) > 0 Then
For Each cc In Selection
sFile = cc.Value
For Each sFolder In aSubFolders
cc.Offset(0, 1) = (Dir(sStartingFolder & sFolder & "\" & sFile) > "")
If cc.Offset(0, 1) Then
' cc.Offset(0, 2) = sFolder
Exit For
End If
Next
Next cc
Else
MsgBox "No subfolders found.", vbInformation
End If

You could return the subfolder name adjacent to the result. I hope this helps.
Peter


2023-06-29 13:16:56

Nicky

Hi, I am trying to make this function work, however my file path is a variable rather than a fixed location. I did try using the whole filepath directly from a cell, but still had the 'Value' error returning against it.

The filepath is fixed until the last folder in the string, which changes depending on which Suppliers' invoice I am trying to check:

Full Path: C:\TEST\2324\Invoices\Supplier Name\filename.pdf

In which both the Supplier Name and filename are variable. I have tried multiple variations, but not one that works, any help would be appreciated :)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.