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

Adding Dashes between Letters

When processing some text data, you may need to perform some esoteric function, such as adding dashes between letters. ...

Discover More

Three-Dimensional Transpositions

Excel makes it easy to transpose your data so that rows become columns and columns rows. It doesn't have a built-in ...

Discover More

Deleting All Comments

Have comments in your document? Want to get rid of them all? The easiest way to do so is going to depend on the ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Saving Information in a Text File

The VBA programming language provide with Excel allows you to create and modify text files quite easily. Here's how to ...

Discover More

Don't Update Links to Other Programs

If you have links in your workbook to data in other workbooks, you may want to control whether Excel updates those links ...

Discover More

Determining If a File Exists

Before you have your macro open and read a file from disk, you'll want to check to make sure it is really there. Here's ...

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 2 + 8?

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.