error on a macro

  • Thread starter Thread starter Exceldude
  • Start date Start date
E

Exceldude

I have created this macro to open an excel file; but if a file is not selected
than end up in error, is there a way to stop the macro without an error
this is what i have

Sub OpenWorkbok()
Dim fName As String
fName = Application.GetOpenFilename()
Workbooks.Open Filename:=fName
End Sub
 
Hi

Sub OpenWorkbok()
Dim fName As String
fName = Application.GetOpenFilename()
If fname <> "" Then
Workbooks.Open Filename:=fName
End If
End Sub
 
Option Explicit
Sub OpenWorkbok()
Dim fName As Variant 'could be the boolean false
fName = Application.GetOpenFilename
if fname = false then
'user hit cancel
else
workbooks.Open Filename:=fName
end if
End Sub
 
Back
Top