Function isFileExist(ByVal fname As String) As Boolean
isFileExist = False
If Dir(fname) <> "" Then isFileExist = True
End Function
Here's example how to use the function above:
Sub FunctionTest()
If isFileExist("D:\SomeFile.txt") = False Then
MsgBox "File not exists."
Else
MsgBox "File already exist."
End If
End Sub
The next function do exactly as previous function, but this function uses FileSystemObject to check whether a file exist:
Function isFileExist2(ByVal fname As String) As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
isFileExist2 = fs.FileExists(fname)
End Function
FIN.Related posts:
- Excel VBA Macro Examples: Sub Procedure
- The MsgBox Function
- Excel VBA Macro Tutorial: Controlling Execution
If you like posts in this blog, you can to support me :)
Here is a different approach basically the same though!
ReplyDeletehttp://vbaexcel.eu/vba-macro-code/determine-if-file-or-directory-exists
I am not a very expert in VBA but i have a little knowledge about the same
ReplyDeleteand i see that this is one of best and easiest way to find the file if its existing or not
good...