Check to see if file exists...

pjmatelli

Board Regular
Joined
Oct 13, 2005
Messages
200
I need help with creating a macro that checks to see if a particular file name exists in the default directory and if it does open the file, if it doesn't exist run another macro....any help is greatly appreciated!
 

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
How about using the Dir() function? Here's an example:

Code:
Sub testExists()
    fname = "parts list.xls"

    If Dir("C:/" & fname & " ") <> "" Then
        Call test1
    Else
        Call test2
    End If

End Sub

Sub test1()
    MsgBox "file exists"
End Sub

Sub test2()
    MsgBox "file does not exist"
End Sub
Hope that helps!
 

pjmatelli

Board Regular
Joined
Oct 13, 2005
Messages
200
Please help...don't know what's wrong with this code...

Sub SaveAsCell()
fname = Sheets("DDE").Range("C" & 1)
If Dir(fname) <> "" Then
Call OpenFile
Else
Call SaveFile
End If
End Sub

Sub OpenFile()
fname = Sheets("DDE").Range("C" & 1)
Workbooks.Open fname
End Sub

Sub SaveFile()
fname = Sheets("DDE").Range("C" & 1)
ActiveWorkbook.SaveAs fname
End Sub
 

Forum statistics

Threads
1,078,012
Messages
5,337,724
Members
399,165
Latest member
ghoshwin

Some videos you may like

This Week's Hot Topics

Top