VBA check if file exists

I have this code. It is supposed to check if a file exists and open it if it does. It does work if the file exists, and if it doesn't, however, whenever I leave the textbox blank and click the submit button, it fails. What I want, if the textbox is blank is to display the error message just like if the file didn't exist.

Runtime-error "1004"

Dim File As String
File = TextBox1.Value
Dim DirFile As String

DirFile = "C:\Documents and Settings\Administrator\Desktop\" & File
If Dir(DirFile) = "" Then
  MsgBox "File does not exist"
Else
    Workbooks.Open Filename:=DirFile
End If

Answers


something like this

best to use a workbook variable to provide further control (if needed) of the opened workbook

Dim strFile As String
Dim WB As Workbook
strFile = Trim(TextBox1.Value)
If Len(strFile) = 0 Then Exit Sub

Dim DirFile As String
DirFile = "C:\Documents and Settings\Administrator\Desktop\" & strFile
If Len(Dir(DirFile)) = 0 Then
  MsgBox "File does not exist"
Else
 Set WB = Workbooks.Open(DirFile)
End If

Need Your Help

How to Align Buttons

java swing layout-manager

buttons are lying horizontally one after the other kindly help me , how to make them come on screen vertically. i tried adding SwingConstants.xxxx then item.SwingConstants.xxx got error horizontal

Why is Jasmine not executing it() on this async test?

javascript tdd jasmine

I'm trying to test a prototypal method that returns insights about a dataset I am loading via AJAX.