vba check if file exist in folder in Excel
Q184. In Excel, how can I check if a file exists in a folder using VBA?
To do it in Excel, here is the answer:
- Option Explicit
- Sub IsFileExists()
- On Error Resume Next
- Dim sFile As String, sTemp As String
- sFile = "C:\Users\Guest\Documents\TestFolder\TestFile.xlsx"
- sTemp = Dir(sFile)
- If sTemp <> "" Then
- MsgBox "File exists."
- MsgBox "File does not exist."
- End If
- End Sub
a) Line 6 - If file exists, Dir(sFile) would return Filename. If it does not exist, it would return empty string. The value of string returned is used to determine the existence of file.