Written by Allen Wyatt (last updated January 13, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
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) FileExists1 = 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" FileExists2 = 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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7402) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Checking for the Existence of a File.
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!
When you use macros to create functions, you might want to share those functions with others�"particularly if they ...
Discover MoreWant to print a document by using a macro? One way is to display the Print dialog box and allow the user to interact with ...
Discover MoreVBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-01-13 11:47:17
J. Woolley
My Excel Toolbox includes the following function to return the last save date/time for a file or folder:
=FileLastDate([FilePath])
FilePath is optional; default is the formula cell's workbook. FilePath can be absolute (like "C:\Users\MyName\Documents\MyFile.xlsx") or relative to the workbook's folder (like "MyFile.xlsx" or ".\SubFolder\MyFile.xlsx" or
"..\SiblingFolder\MyFile.xlsx"). FilePath might reference a folder instead of a file (like "." for the workbook's folder or "C:\Users\MyName\Documents"). If the file or folder cannot be found, a #VALUE! error is returned.
See https://sites.google.com/view/MyExcelToolbox
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments