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
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
Try this macro:Replace "Book4.xlsx" with the name of the file you want to check.![]()
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
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.
I've been unable to get this to recognize my file (Inventory.xls) located in the directory the spreadsheet was opened from.
Thanks
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
![]()
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.
I was able to get this to work:
Thanks for your help!![]()
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks