Check a File Existence and popup an Message saying weather the file is Found or Not Found

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
Hi

I have a question for you,

I'm want to run a macro where, the macro will check for the inputs given in a sheet and execute the codes, say

In Cell - B1 = FileName.doc
In Cell - B2 = C:\Directory\Folder\

Code:
Sub Check_File ()
     Dim FileName, FilePath, FileCheck as String

    FileName = Sheet1.Range("B1").Value
    FilePath = Sheet1.Range("B2").Value
    
    If FilePath = "Found" then
        If FileName = "Found" then
            Msg Box "File Found in Given Path"
            Execute Some Code
        Else
            MsgBox "File Not Found in Given Path"
        End If
    Else
            MsgBox "Given File Path Not Found"
    End If
 End Sub

I'm wondering, Is there a way to that.

Thanks in advance...
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,

Try something like this:
Code:
Sub Check_File()
    Dim FileName, FilePath, FileCheck As String

    FileName = Sheet1.Range("B1").Value
    FilePath = Sheet1.Range("B2").Value
    
    If Dir(FilePath, vbDirectory) = "" Then
        MsgBox "Given File Path Not Found"
    Else
        If Dir(FilePath & "\" & FileName) = "" Then
            MsgBox "File Not Found in Given Path"
        Else
            MsgBox "File Found in Given Path"
            'Execute Some Code
        End If
    End If
End Sub
 
Upvote 0
@ Debeuz

Thanks for the reply

If you don't mind, I have one doubt, Can we create the Folder Structure if it not existing

Like If I'm Searching for a Path like,
C:\Directory\Folder\Sub Folder\My Folder

In which "Sub Folder" is not existing, then can we create the "Sub Folder" under "Folder and then "My Folder" under "Sub Folder"?

In this example I have given 4 folders, if we have more folders like this can we loop and create complete Folder Structure..??
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,995
Members
449,480
Latest member
yesitisasport

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