VBA to check file exists

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I have a macro that opens a file and pulls in data,
I would like to add a bit of code that checks that file exists, if not exit sub

please help if you can

Thanks
Tony
Here my code:

VBA Code:
Sub Open_Dell()


 With Workbooks.Open(ThisWorkbook.Path & "\Dell Notebooks.xlsx", UpdateLinks:=0)
 
            With .Sheets("Layout")
               .Range("A2:D" & .Range("A" & Rows.Count).End(xlUp).Row).Copy
            
           Lr1 = ThisWorkbook.Sheets("Price Lists").Cells(Rows.Count, "A").End(xlUp).Row + 2
           ThisWorkbook.Sheets("Price Lists").Range("A" & Lr1).PasteSpecial xlPasteValues
           
           Application.CutCopyMode = False
            
           End With
           .Close False
End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How about
VBA Code:
Sub Open_Dell()
Dim Fname As String
   

Fname = Dir(ThisWorkbook.path & "\Dell Notebooks.xlsx")
If Fname = "" Then Exit Sub
With Workbooks.Open(Fname, UpdateLinks:=0)
 
            With .Sheets("Layout")
               .Range("A2:D" & .Range("A" & Rows.Count).End(xlUp).Row).Copy
            
           Lr1 = ThisWorkbook.Sheets("Price Lists").Cells(Rows.Count, "A").End(xlUp).Row + 2
           ThisWorkbook.Sheets("Price Lists").Range("A" & Lr1).PasteSpecial xlPasteValues
           
           Application.CutCopyMode = False
            
           End With
           .Close False
End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Can this be made quicker or better by checking if open and then getting data. Data > Get Data from File Workbook

Not sure of quickest way to get data from closed book
 
Upvote 0
As this is a totally different from the op, you need to start a thread of your own. Thanks
 
Upvote 0
ok will do

PS I got an error with the line below. The file is there because it passed If Fname = "" Then Exit Sub

With Workbooks.Open(Fname, UpdateLinks:=0)

Says Run time error 1004 Sorry we couldnt find test.xlsx. Is it possible it was moved renamed or deleted
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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