I have spent my whole morning on this and cannot get it working properly. A simple Excel userform was created asking for a filename. If the file exists in the directory I want it to open. If it does not exist I want a "template" file opened instead. I have the does not exist working properly, however cannot get the "does exist" part working. Please help.
Private Sub CmdEnter_Click()
Dim Path As String
Dim File As String
Path = Range("Search!B1")
File = TxtOrder.Value
'If File exists then open.
If Dir(Path & File & ".xlsm") = Path & File & ".xlsm" Then
Workbooks.Open FileName:=Path & File & ".xlsm"
'If File does not exist then open.
ElseIf Dir(Path & File & ".xlsm") = Error Then
Workbooks.Open FileName:=Path & "QCSFormTrial.xlsm"
End If
'Close Dialog and Close Workbook
Workbooks("QCSLaunch.XLSM").Close SaveChanges:=False
End Sub
Pathcontain? If it contains a string ending in "\", you should useIf Dir(Path & File & ".xlsm") <> "" Then. Instead ofElseIf Dir(Path & File & ".xlsm") = Error Thenuse justElse.Dir(Path & File & ".xlsm") = Errordoes not have any meaning in VBA.Dir()returns a path or an empty string. Never a error and even if it should do so, that way of checking for error does not exist in VBA.wb) which can be any of the two. Your code will raise an error if the first condition isTrue..."QCSLaunch.XLSM"related to the code? Is it the workbook containing the cell with the path? Is it the workbook containing this code? Why are you closing it?