forked from aaronkeene/ExcelVBA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileExist.vba
56 lines (35 loc) · 1.14 KB
/
FileExist.vba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Function FileExist( _
ByVal FilePath As String, _
ByVal FileName As String, _
Optional ByVal FileType As String = "NotGiven") As Boolean
Dim fName As String
FileExist = False
FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
If FileType <> "NotGiven" Then
If Right(FileName, 1) = "." Then
FileName = Left(FileName, Len(FileName) - 1)
End If
If Left(FileType, 1) <> "." Then
FileType = "." & FileType
End If
fName = FilePath & FileName & FileType
Else
fName = FilePath & FileName
End If
If Dir(fName) <> "" Then
FileExist = True
End If
End Function
Sub SampleUsage()
'Sample usage for FileExist function
' Note: file extension is included in the filename
Dim fPath As String 'Directory where files should be
Dim fName As String 'File name (pulled from spreadsheet)
fPath = "C:\"
fName = "Test.xls"
If FileExist(fPath, fName) Then
MsgBox "File Found"
Else
MsgBox "File Not Found"
End If
End Sub