+ Reply to Thread
Results 1 to 8 of 8

VBA Loop - Does file exist (Y/N) at the path specified?

  1. #1
    Registered User
    Join Date
    06-09-2012
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    9

    VBA Loop - Does file exist (Y/N) at the path specified?

    On a sheet named Directory

    Starting in A2, I have a list of paths, starting in B2 I have a list of file names.

    I'd like a macro that will go to the correct path, and see if the file exist. If it does, then put a Y in G2, if it does not put a N in G2, the number of rows used will vary.

    Thanks,

  2. #2
    Registered User
    Join Date
    05-09-2012
    Location
    Des Moines, IA
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    Try this as a function:
    Please Login or Register  to view this content.
    Now in C2 enter the formula "=FileExists(A2 & "\" & B2)". This should give you the answer you want. You can do a paste values if you don't want formulas.
    You can also use the function in a macro to loop through the cells you want.

  3. #3
    Registered User
    Join Date
    06-09-2012
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    Here's what I ended up using...took me a bit to figure it out...

    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 TestFolderExistence()

    Dim FileRow As Integer
    Dim RowCount As Integer

    FileRow = 2
    RowCount = Range("A2:A" & Range("A2").SpecialCells(xlLastCell).Row).Rows.Count

    Do Until FileRow = RowCount
    If FileFolderExists(Range("A" & FileRow).Value & "\" & Range("B" & FileRow).Value) Then
    Range("G" & FileRow).Formula = "Y"
    Else
    Range("G" & FileRow).Formula = "N"
    End If
    FileRow = FileRow + 1
    Loop

    End Sub

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    That's a little more complicated than you need:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Guru tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    I don't think the user created that, I'm pretty sure the code is pulled from here:
    http://www.excelguru.ca/content.php?157
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  6. #6
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    Interesting. I don't get an error on a removed removable drive ...

  7. #7
    Registered User
    Join Date
    06-09-2012
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    Nope, I didn't create it, wasn't trying to claim it as my own...I just tweaked other code I found, was showing how I tweaked it.

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Loop - Does file exist (Y/N) at the path specified?

    Understood. As a courtesy, though, you should make a point of crediting the source -- it's the nice thing to do.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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