ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   New Users to Excel (https://www.excelbanter.com/new-users-excel/)
-   -   error on a macro (https://www.excelbanter.com/new-users-excel/205874-error-macro.html)

Exceldude

error on a macro
 
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



Roger Govier[_3_]

error on a macro
 
Hi

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


--
Regards
Roger Govier

"Exceldude" wrote in message
...
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



Dave Peterson

error on a macro
 
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

Exceldude wrote:

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


--

Dave Peterson

Exceldude

error on a macro
 
Thank you Dave,
that was exactly what I needed; it works Great


"Dave Peterson" wrote:

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

Exceldude wrote:

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


--

Dave Peterson



All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com