error on a macro

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
 
R

Roger Govier

Hi

Sub OpenWorkbok()
Dim fName As String
fName = Application.GetOpenFilename()
If fname <> "" Then
Workbooks.Open Filename:=fName
End If
End Sub
 
D

Dave Peterson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top