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
 
Do you know why that line would be causing the problem?
Not sure why it was causing you a problem, as it didn't for me. I just get the dialogue box twice.
 
Last edited:
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Fluff, do you know if there's a way to edit this code so that it opens a file called "Update" in the Downloads folder and imports the data from that file without a user having to manually select the file?

ie the Sub just carries out all the actions by itself, including going to the folder, selecting the file and getting the data?

VBA Code:
Sub Import()





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") & "\Downloads\



If fd.Show class=prism-token><> -1 class=prism-token>Then
   MsgBox class=prism-token>"You didn't select a file?"
   class=prism-token>Exit Sub
class=prism-token>End If


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
 
Upvote 0
As this is a totally different question, you need to start a thread of your own.
 
Upvote 0

Forum statistics

Threads
1,212,933
Messages
6,110,752
Members
448,295
Latest member
Uzair Tahir Khan

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