Check if file exists / open it

G

Gert-Jan

For some extra functions I created a second workbook (wb2.xls). In Sheet1!A1
(wb1.xls) is the filename (created with a formula). This works fine:

Sub Test1()
Workbooks.Open Filename:= _
Range("Sheet1!A1").Value
End Sub

But this doesn't work:

Sub Test2()
Option Explicit
Dim mywkbk As Workbook
Dim myfile As String
myfile = Range("sheet1!A1").Value
If Dir(myfile) = "" Then
MsgBox "that doesn't exist!"
Exit Sub
Else
Set mywkbk = Workbooks.Open(myfile)
End If
End Sub

Can someone help??
 
D

Die_Another_Day

Try this:
If Not Len(Dir(myfile)) > 0 Then
MsgBox "that doesn't exist"
Else....

Charles
 
G

Guest

There are a couple of problems I see in your code. Give this a try...

Option Explicit

Sub Test2()
Dim mywkbk As Workbook
Dim myfile As String

myfile = Worksheets("Sheet1").Range("A1").Value
If Dir(myfile) = "" Then
MsgBox "that doesn't exist!"
Else
Set mywkbk = Workbooks.Open(myfile)
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top