Show dialog if file not found help

InptTrdr

New Member
Joined
May 16, 2011
Messages
8
Hello All,

I've had no luck searching for similar posting, so here I am...

I'm attempting to create a macro where by a particular file will be opened as defined by the code, but if it cannot find it to open the dialog box for the user to select the file. The code I have below works, except if I click cancel on the open dialog box, the macro continues on regardless and executes on whatever file is currently open (not good). My question is, how do I end the sub if I click cancel?

Code:
Sub report()

Dim sfile As String

sfile = Dir("C:\report.xls")

If sfile <> "" Then Workbooks.Open Filename:="C:\report.xls" _
Else Application.Dialogs(xlDialogFindFile).Show

'simple formating macro below...

End Sub

Thanks you in advance for any assistance

Inpttrdr (ignorant noob)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Application.Dialogs(xlDialogFindFile).Show will return False if Cancel. Also (I think) that DialogFindFile might not open the file, so I substituted the Open file dialog.
Try,
Code:
If sfile <> "" Then 
    Workbooks.Open Filename:="C:\report.xls" _
Else 
    If Not (Application.Dialogs(xlDialogOpen).Show) Then Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top