Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default Check if file exists / open it

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??


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Check if file exists / open it

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

Charles

Gert-Jan wrote:
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??


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Check if file exists / open it

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

--
HTH...

Jim Thomlinson


"Gert-Jan" wrote:

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??



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if file exists Jon Excel Discussion (Misc queries) 14 October 4th 07 04:57 PM
How do I check if a sheetname exists in a file? NezRhodes Excel Programming 2 August 22nd 06 09:44 AM
How to check if a file exists in an ftp folder LL Cool A Excel Discussion (Misc queries) 3 May 16th 06 09:22 PM
check if file exists Curt Excel Programming 0 December 7th 05 05:02 PM
check if a file exists / is open Mark Kubicki Excel Programming 1 December 4th 03 05:56 PM


All times are GMT +1. The time now is 09:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"