Exit Sub if a user doesn't select a file?

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've got a Sub which allows a user to import data from a file, but I need the Sub to stop running if a user doesn't select a file, when a folder is opened.

Does anyone remember how to do this?

I'd be grateful if you could advise where to insert the amendment in the code below, please?

TIA

VBA Code:
Sub Import1()


Dim fd As FileDialog
Dim filewaschosen As Boolean
Dim Report As Workbook
Dim iWB As Workbook


Set Report = ActiveWorkbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
'fd.Filters.Add "xlsx files", "*.xlsx"
fd.Filters.Add "Custom Excel Files", "*.xlsx, *.xlsm, *.xls"
fd.AllowMultiSelect = False
fd.InitialFileName = Environ("UserProfile") & "\Box\Proposals & Contracts"
filewaschosen = fd.Show
fd.Execute

lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:G" & lastrow).Copy
Report.Activate
ACV.Visible = True
ACV.Activate
Range("A2:G" & lastrow).PasteSpecial

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
VBA Code:
Sub Import1()


Dim fd As FileDialog
Dim filewaschosen As Boolean
Dim Report As Workbook
Dim iWB As Workbook


Set Report = ActiveWorkbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
'fd.Filters.Add "xlsx files", "*.xlsx"
fd.Filters.Add "Custom Excel Files", "*.xlsx, *.xlsm, *.xls"
fd.AllowMultiSelect = False
fd.InitialFileName = Environ("UserProfile") & "\Box\Proposals & Contracts"
filewaschosen = fd.Show
fd.Execute

[COLOR=rgb(209, 72, 65)]If fd = False Then Exit Sub[/COLOR]

lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:G" & lastrow).Copy
Report.Activate
ACV.Visible = True
ACV.Activate
Range("A2:G" & lastrow).PasteSpecial

End Sub
 
Upvote 0
Thanks for the prompt response, @excel_newbie86.

I tried that, but it didn't work....it produced an error.

I'd also like to add in a message box to tell a user that they didn't select a file. So this is what I have, at the moment.

If anyone knows how to amend this, please let me know:


VBA Code:
Sub Import1()


Dim fd As FileDialog
Dim filewaschosen As Boolean
Dim Report As Workbook
Dim iWB As Workbook



Set Report = ActiveWorkbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
'fd.Filters.Add "xlsx files", "*.xlsx"
fd.Filters.Add "Custom Excel Files", "*.xlsx, *.xlsm, *.xls"
fd.AllowMultiSelect = False
fd.InitialFileName = Environ("UserProfile") & "\Box\Proposals & Contracts"
filewaschosen = fd.Show
fd.Execute

If fd = False Then

MsgBox "You didn't select a file?"

Exit Sub

End If

lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:G" & lastrow).Copy
Report.Activate
ACV.Visible = True
ACV.Activate
Range("A2:G" & lastrow).PasteSpecial

End Sub
 
Upvote 0
How about
VBA Code:
fd.InitialFileName = Environ("UserProfile") & "\Box\Proposals & Contracts\"
If fd.Show <> -1 Then
   MsgBox "You didn't select a file?"
   Exit Sub
End If
fd.Execute
 
Upvote 0
Yes!!!

That's exactly what I'm looking for, @Fluff !

Thank you - that worked!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Hi Fluff

The code you wrote above worked on the Sub I was working on at the time, so thank you for that.

I'm trying to add it to a different Sub, I'm getting an error presumably because I've put it in the wrong place in this Sub.

Do you know where I should place it in this Sub, please?

VBA Code:
Sub GetData()

Application.ScreenUpdating = False

Dim fd As FileDialog
Dim filewaschosen As Boolean
Dim Report As Workbook
Dim iWB As Workbook



Set Report = ActiveWorkbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "Custom Excel Files", "*.xlsx, *.xlsm, *.xls"
fd.AllowMultiSelect = False
fd.InitialFileName = Environ("UserProfile") & "\Box\RelevantFolder"
filewaschosen = fd.Show
If fd.Show <> -1 Then
   MsgBox "You didn't select a file?"
   Exit Sub
End If
fd.Execute

Set iWB = ActiveWorkbook
 
Upvote 0
Try removing this line filewaschosen = fd.Show
 
Upvote 0
Hi Fluff

Apologies for the delay in responding, but that worked! Thank you!

Do you know why that line would be causing the problem?

I'm just curious...

Thanks again for an awesome solution!
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,710
Members
448,293
Latest member
jin kazuya

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