-2

I am trying to run a macro to check if a file exists. I get compiling error that Sub or function not defined. Can someone help please

If FileExists(filepath) Then

Else
  Call Master
  Call PrinttoPDF
End If
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
FUNDMANAGER
  • 1
  • 1
  • 4

2 Answers2

1

Try following sub.

Sub CheckFilePath()
    If Dir(FilePath, vbNormal) <> "" Then
        Call Master
        Call PrinttoPDF
    Else
        MsgBox "File does not exists."
    End If
End Sub
Harun24hr
  • 30,391
  • 4
  • 21
  • 36
  • Hi. Actually the variable FilePath is the name of the file along with its full path. The suggested change is running the else part even when the file exists in the path location. Any further change you would suggest? – FUNDMANAGER Mar 14 '19 at 09:03
  • I have tested the code and just running fine. Please double check your file path. Can you please put full code here to see. – Harun24hr Mar 14 '19 at 09:59
0

I'm no VBA guru, but it looks like either FileExists, Master, or PrinttoPDF doesn't exist as a Sub or Function. Maybe change the Case, the last one should probably be PrintToPdf.

I would expect the error to tell you which line the error occurred on.

There is a working example I found on this page that you could work through:

Sub Test_File_Exist_With_Dir()
    Application.ScreenUpdating = False
    Dim FilePath As String
    FilePath = ""
    On Error Resume Next
    FilePath = Dir("C:\Users\DT168\Desktop\Test folder\Book2.xlsx")
    On Error GoTo 0
    If FilePath = "" Then
        MsgBox "File doesn't exist", vbInformation, "Kutools for Excel"
    Else
        MsgBox "File exist", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = False
End Sub
Matt D
  • 3,289
  • 1
  • 15
  • 29