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:

  1. Option Explicit
  2. Sub IsFileExists()
  3. On Error Resume Next
  4. Dim sFile As String, sTemp As String
  5. sFile = "C:\Users\Guest\Documents\TestFolder\TestFile.xlsx"
  6. sTemp = Dir(sFile)
  7. If sTemp <> "" Then
  8. MsgBox "File exists."
  9. Else
  10. MsgBox "File does not exist."
  11. End If
  12. 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.


You can find similar Excel Questions and Answer hereunder

1) How to add a link in a sheet to another sheet

2) Vba list all files in a folder in Excel

3) How can I avoid updates to cell values during macro execution?

4) How can I save a WorkSheet as a new WorkBook using VBA?

5) How can I dynamically add a hyperlink using VBA?

6) How can I set up ListBox using VBA to allow users to select multiple values?

7) Vba to return week numbers in Excel

8) How can I add a WorkSheet and name it as required using VBA?

9) How can I update a listbox based on data in a list using VBA?

10) How do I enter a formula in a cell using VBA (using Relative Reference)?

11) How can I delete all shapes in a WorkSheet?

12) How to concatenate strings in vba in Excel

13) How can I export a WorkSheet as a PDF using VBA?

14) How can I activate a routine when there is a change in value of a cell?

15) String split in vba in Excel