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

Cropping Graphics

Need your hide some of the outside edges of a graphic? You can instruct Word to crop (or hide) those outside edges by ...

Discover More

Word's Native Measurement Unit

Word allows you to specify distances using a number of different measurement units. Figuring out how those measurement ...

Discover More

Counting Times within a Range

Excel allows you to easily store dates and times in your worksheets. If you have a range of cells that contain times and ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Sudden Increases in Workbook File Size

Workbooks can get rather large rather quickly. If you think your workbook has gotten too big too fast, here are some ...

Discover More

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

Full Path Names in Excel

Need to know what the full path name is for the current workbook? With a simple macro you can display the full path name ...

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

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.