Excel code for File Exist Issue

Anonymous
2022-08-27T07:18:29+00:00

I am not much familiar with VBA code in Excel.

I have got this code for my task- to check file exist in folder and if yes- it will show "Yes" with conditional formula.

The code is working fine except one issue- Its not automatically updating the file when present, specially when the Excel is open.

The code is

Function FileExists(full_path As String)

Dim fso_obj As Object

Set fso_obj = CreateObject("Scripting.FileSystemObject")

FileExists = fso_obj.FileExists(full_path)

End Function

Using this conditional formula to show "Yes"

=IF(FileExists(K5),"YES", "")

Thanks in advance

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} vote
Answer accepted by question author
  1. Andreas Killer 144K Reputation points Volunteer Moderator
    2022-08-27T11:18:25+00:00

    Function FileExists(full_path As String) As Boolean
    Static fso_obj As Object
    Application.Volatile
    If fso_obj Is Nothing Then
    Set fso_obj = CreateObject("Scripting.FileSystemObject")
    End If
    FileExists = fso_obj.FileExists(full_path)
    End Function

    2 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-08-27T13:35:29+00:00

    Thanks Andreas,

    Still some of the files I need to manually click on the conditional formula to update.

    Second thing I have pasted the code in Module of This Workbook. Is it the right path?

    0 comments No comments
  2. Andreas Killer 144K Reputation points Volunteer Moderator
    2022-08-27T19:42:43+00:00
    1. remove your existing code!
    2. the code must be placed in a regular module
    3. such things work only if called as UDF from a cell
    0 comments No comments
  3. Anonymous
    2022-08-28T13:19:42+00:00

    I did the removal of my previous code and placed your code.

    And yes, it's in the regular module of the workbook.

    What I found here is if I write the code (=IF(FileExists(C2),"YES","NO",) in the cell, the excel is not automatically updating if the file exists or is not exist in the concerned folder. (Column-E in below image-File-2.docx exist but the cell value is not updating automatically).

    Need to click some cells in excel to update the excel. Sometimes it works & sometimes not.

    However, I also found that if the code (=FileExist(A2)), is entered in conditional formatting with highlighting color, then the code is working perfectly. (Column-D in below image- both file exists in folder)

    By the way, thanks for the help.

    0 comments No comments