+ Reply to Thread
Results 1 to 5 of 5

Thread: Checking if a file exists

  1. #1
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Checking if a file exists

    Hi,

    I am trying to check if a file exists and then return an error message if it does not, however this is not working and the code is continuing regardless of weather or not the file is there.

    Any hlep greatly appriciated

    Thanks,
    David


    CheckFile = FilePath & FileName
    If CheckFile = "" Then
        MsgBox "The file Employee Change Updates could not be found."
        Exit Sub
    End If

  2. #2
    Valued Forum Contributor
    Join Date
    03-25-2008
    MS-Off Ver
    Excel, Outlook, Word 2007/2003
    Posts
    246
    Sub check_file()
    Dim mypath As String
    Dim myfiletocheck As String
    Dim myfile As String
    myfile = Dir(mypath & Application.PathSeparator & myfiletocheck)
    If myfile <> vbNullString Then
        MsgBox myfile & " is present", vbInformation
    Else
        MsgBox "The file : " & myfile & " is not available.", vbExclamation
    End If
    End Sub
    Charlize

  3. #3
    Forum Moderator dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003 & 2007
    Posts
    3,714

    Smile

    Good morning duckboy1981

    Alternatively you could write it as a function and call as required :
    Private Function DoesFileExist(FleNme) As Boolean
    DoesFileExist = (Dir(FleNme) <> "")
    End Function
    
    Sub test()
    If DoesFileExist("D:\Work\MyFile.xls") = True Then
    '   ... Your code here ...
    End If
    End Sub
    HTH

    DominicB

  4. #4
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185
    Sweet, nice and easy - just what i needed! Thanks

  5. #5
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566
    Hi,

    If (FilePath & Filename) <> "" Then
       MsgBox "File Employee Change Exists"
       Else
       MsgBox "File Employee Change doesn't exist"
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Detect if File is opened on USB Drive
    By tekman in forum Excel Programming
    Replies: 0
    Last Post: 05-02-2008, 09:56 PM
  2. Checking To See If File Is Already Open
    By Launchnet in forum Excel Programming
    Replies: 8
    Last Post: 04-12-2008, 11:54 AM
  3. Using String as filename in Save As not recognised by Dir
    By NoCanDo in forum Excel Programming
    Replies: 4
    Last Post: 03-20-2008, 05:49 PM
  4. mutual exclusive write to Excel file
    By extreme in forum Excel Programming
    Replies: 0
    Last Post: 06-13-2007, 05:18 AM
  5. DIR code problem - Checking a file exists
    By LB79 in forum Excel General
    Replies: 6
    Last Post: 09-22-2006, 09:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0