If file exists then cell value equals "Yes"

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Morning everyone, I am trying to look up a file path (http:) to check if a file exists, if it does a folrmula in a cell will return a value if True and antoher value if False. I thinking a functin and a UDf will do the trick, but dont know where to start. To make things harder the file path will never be the same. each file path is created by different cell values. See below:
Code:
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
("[URL]http://abcdefgwebsite/[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")

I really appreciate any direction anyone give me :)

sd
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Morning everyone, I am trying to look up a file path (http:) to check if a file exists, if it does a folrmula in a cell will return a value if True and antoher value if False. I thinking a functin and a UDf will do the trick, but dont know where to start. To make things harder the file path will never be the same. each file path is created by different cell values. See below:
Code:
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
("[URL]http://abcdefgwebsite/[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")

I really appreciate any direction anyone give me :)

sd


I was thinking a function like so might get me started?
Code:
Function FileExists(FullpathName As String) As Boolean
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
If Dir("[URL]http://abcdefgwebsite[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx") <> "" Then
    FileExists = True
Else
    FileExists = False
End If
End Function
 
Last edited by a moderator:
Upvote 0
Ive tried the above function with:

IF(FileExists,"have","have not") as the formula, with no luck...

Any ideas?

sd
 
Upvote 0
Would someone be able to take a look at this, to help come up with a solution?

Thank you so much in advance for any help with this. :)
sd
 
Upvote 0
I have tried this sub and function to test with a messge box, but even though the file is there, it returns a false answer? Any ideas?

Code:
Public Function FileFolderExists(strFullPath As String) As Boolean
    On Error GoTo EarlyExit
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    
EarlyExit:
    On Error GoTo 0
End Function
Public Sub TestFileExistence()
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
    If FileFolderExists("abcdefg/[URL="http://infonet.t-mobile.com/sites/retail/WFM/"]sites/retail/WFM/[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx") Then
        MsgBox "File exists!"
    Else
        MsgBox "File does not exist!"
    End If
End Sub

I would really appreciate any help :)

sd
 
Upvote 0
Try this (untested):-
Code:
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
fname=("http://abcdefgwebsite/" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
if fileexists(fname)=true then
     'do whatever you need to here
else
     'do something else
endif

This will assign the complete file and path the the variable fname

to check if the file exists then try:-
Code:
Private Function FileExists(fname) As Boolean
'----- Returns TRUE if the file exists -----
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True Else FileExists = False
End Function

This will work on files held on networks, I'm not entirely sure what will happen if you're checking a website.

You can then test if FileExists is true or not (i.e. the file exists) and
 
Upvote 0
Try this (untested):-
Code:
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
fname=("http://abcdefgwebsite/" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
if fileexists(fname)=true then
     'do whatever you need to here
else
     'do something else
endif

This will assign the complete file and path the the variable fname

to check if the file exists then try:-
Code:
Private Function FileExists(fname) As Boolean
'----- Returns TRUE if the file exists -----
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True Else FileExists = False
End Function

This will work on files held on networks, I'm not entirely sure what will happen if you're checking a website.

You can then test if FileExists is true or not (i.e. the file exists) and


First, thank you so much for the help!! :) I have tried this, and get hung up on:
Code:
 x = Dir(fname)
says is a bad file name or number (runtime error)

Any ideas?

sd
 
Upvote 0
Try this (untested):-
Rich (BB code):
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
fname=("http://abcdefgwebsite/" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
if fileexists(fname)=true then
     'do whatever you need to here
else
     'do something else
endif

This will assign the complete file and path the the variable fname

to check if the file exists then try:-
Rich (BB code):
Private Function FileExists(fname) As Boolean
'----- Returns TRUE if the file exists -----
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True Else FileExists = False
End Function

This will work on files held on networks, I'm not entirely sure what will happen if you're checking a website.

You can then test if FileExists is true or not (i.e. the file exists) and


Hi Richard i modified your function and have not used the sub to try to get a cell to return true or false. but no luck :( any ideas?

The function was changed to:
Code:
Public Function FileExists() As Boolean
'----- Returns TRUE if the file exists -----
ThisFile = Worksheets("MyStoreInfo").Range("C2")
Area = Worksheets("MyStoreInfo").Range("E8")
Region = Worksheets("MyStoreInfo").Range("F8")
District = Worksheets("MyStoreInfo").Range("C8")
M0nth = Worksheets("Dashboard").Range("O8")
Dim x As String
x = Dir("[URL]http://abcwebsite/[/URL]" & Area & "/" & Region & "/" & District & "/" & M0nth & "Schedule" & ThisFile & ".xlsx")
    
    If x <> "" Then FileExists = True Else FileExists = False
    
End Function

and the cell formula is:
Code:
=IF(FileExists,"have","have not")

Thanks again for any help.

sd
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top