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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Wouldn't it be nice to have a single command that would save each of you open workbooks, all at once? It's easy to do ...
Discover MoreImporting a single file is easy. Importing a whole slew of files can be much more of a challenge.
Discover MoreThe Open dialog box allows you to sort the files it presents to you. How you do the sorting depends on the version of ...
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