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.
Written by Allen Wyatt (last updated May 8, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Workbooks can get rather large rather quickly. If you think your workbook has gotten too big too fast, here are some ...
Discover MoreIf you need to combine the contents of a bunch of workbooks into a single workbook, the process can get tedious. Here's a ...
Discover MoreIf you use Excel's Open dialog box to search for files and you notice that doing so ends up crashing your system, you may ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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 :)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments