+ Reply to Thread
Results 1 to 6 of 6

Check if file exists from same folder spreadsheet was opened

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2006
    Location
    Detroit area, Michigan
    MS-Off Ver
    Microsoft Office 365 v16
    Posts
    68

    Check if file exists from same folder spreadsheet was opened

    I have a spreadsheet that may be opened from different folders.

    Is it possible to create a macro to see if a file exists in the same folder that the spreadsheet was opened from?

    Thanks

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Check if file exists from same folder spreadsheet was opened

    Try this macro:
    Sub FindFile()
        Dim MyFolder As String
        Dim MyFile As String
        MyFolder = ThisWorkbook.Path
        MyFile = Dir(MyFolder & "\" & "*.xls*")
        Do While MyFile <> ""
            If MyFile = "Book4.xlsx" Then
                MsgBox MyFile & " exists."
            End If
            MyFile = Dir
        Loop
    End Sub
    Replace "Book4.xlsx" with the name of the file you want to check.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    06-20-2006
    Location
    Detroit area, Michigan
    MS-Off Ver
    Microsoft Office 365 v16
    Posts
    68

    Re: Check if file exists from same folder spreadsheet was opened

    I've been unable to get this to recognize my file (Inventory.xls) located in the directory the spreadsheet was opened from.

    Thanks

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Check if file exists from same folder spreadsheet was opened

    Just tried it and the code works like it should.
    Did you type in the name exactly as is? No extra spaces, capital letters etc

  5. #5
    Registered User
    Join Date
    07-21-2015
    Location
    Earth
    MS-Off Ver
    2013
    Posts
    28

    Re: Check if file exists from same folder spreadsheet was opened

    Sub Find_file()
    checkfile = InputBox("Pls type the file name:")
    If checkfile = "" Then Exit Sub
    If Len(Dir(ThisWorkbook.Path & "\" & checkfile, vbDirectory)) > 0 Then
    MsgBox "File " & checkfile & " existed"
    Else
    MsgBox "Nothing found."
    End If
    End Sub
    Last edited by gaixixon; 08-07-2016 at 11:06 PM.

  6. #6
    Registered User
    Join Date
    06-20-2006
    Location
    Detroit area, Michigan
    MS-Off Ver
    Microsoft Office 365 v16
    Posts
    68

    Re: Check if file exists from same folder spreadsheet was opened

    I was able to get this to work:

    Sub Find_file()
    'checkfile = InputBox("Pls type the file name:")
    checkfile = "Inventory.xls"
    If checkfile = "" Then Exit Sub
    If Len(Dir(ThisWorkbook.Path & "\" & checkfile, vbDirectory)) > 0 Then
    MsgBox "File " & checkfile & " existed"
    Else
    MsgBox "Nothing found."
    End If
    End Sub
    Thanks for your help!

+ 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. [SOLVED] Check if folder exists, Create if Not
    By szpt9m in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2014, 04:03 AM
  2. Check if Folder exists
    By akq125 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-02-2012, 10:35 PM
  3. Check if a mailbox folder exists
    By fcomino in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-24-2010, 09:08 AM
  4. Check if folder exists, if not create it
    By mattmac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2009, 10:17 AM
  5. Check if folder exists, if yes just copy sheet in to folder?
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2006, 10:44 AM
  6. [SOLVED] How to check if a file exists in an ftp folder
    By LL Cool A in forum Excel General
    Replies: 3
    Last Post: 05-16-2006, 04:25 PM
  7. how to check if folder exists
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-14-2005, 07:03 AM

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.6.0 RC 1