Refine/amend VBA to open files

SaraB802

New Member
Joined
Feb 7, 2013
Messages
34
Hi, so I have the below code which opens a source file and then copies and pastes the data in to the original file. I need to change it to so that if the cell (B3) is empty then the macro stops, without errors. I need it to continue as it does if the file name/path in B3 isn't found.

Any help much appreciated

Dim ExtFile As String
Dim ExtBk As Workbook

ExtFile = Range("B3").Value
If Not ExtFile = "" And Dir(ExtFile) <> "" Then
Else
ExtFile = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xls), *.xls", Title:="Please Select A File")
End If
On Error Resume Next
Set ExtBk = Workbooks(Dir(ExtFile))
On Error GoTo 0
If ExtBk Is Nothing Then
Application.Workbooks.Open ExtFile
Set ExtBk = Workbooks(Dir(ExtFile))
End If
'TO HERE OPENS THE FIRST FILE


Worksheets("database").Select
Sheets("Database").UsedRange.Select
Selection.Copy


' select current workbook and paste the values starting at A1
Windows("14.03 Portfolio Rollup Basic.xlsm").Activate
Worksheets("Project 1").Select
Sheets("Project 1").Range("A1").Select
Worksheets("Project 1").Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormat
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Re: HELP to refine/amend VBA to open files

After this line:
Code:
[COLOR=#333333]ExtFile = Range("B3").Value[/COLOR]
You should just be able to add this line right after it:
Code:
If ExtFile = "" Then Exit Sub
 
Upvote 0
Re: HELP to refine/amend VBA to open files

Thanks, I am slowly learning bits of VBA by trial and error.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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