+ Reply to Thread
Results 1 to 6 of 6

Find a file in a director and return "true" if found

  1. #1
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Find a file in a director and return "true" if found

    This should be pretty easy for all you VBA pros.

    In a VBA Sub I create a file that its name is prefixed with a variable found in a cell. When the file is created a put it in a specific directory. This new code I want to develop looks for this file first before I create it again. The file can be found in:

    C:\Users\Bob\Documents\"cellvariable" & code.txt

    And "cellvariable" is a number found in a cell on a worksheet i.e. 12345. I simply want an IF statement that looks for the file in that directory, then if not found Calls another Sub that will then create the file. If found it does nothing meaning it does not create the file.

    Thanks in advance.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,826

    Re: Find a file in a director and return "true" if found

    Can you post a copy of your file including the macro you are currently using?
    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.

  3. #3
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Find a file in a director and return "true" if found

    Quote Originally Posted by Mumps1 View Post
    Can you post a copy of your file including the macro you are currently using?
    Here is the code to create the file:
    Please Login or Register  to view this content.
    I do not have any code written to see if that file already exists in that directory: C:\Users\Bob\Documents\Debug\

    The file itself is just a text file with numbers in it. The file will be "Cell D9"OutputdataCode.txt where Cell D9 is a number found in Cell D9 of the worksheet "Data Entry".

    Does that help?

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Find a file in a director and return "true" if found

    There are two good ways I know of. One uses 'dir' and the other uses a 'fileSystemObject'.

    I like using the FSO approach: Example:
    Please Login or Register  to view this content.

    Other method, would be something like this:

    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 08-29-2016 at 03:25 PM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,826

    Re: Find a file in a director and return "true" if found

    Try this macro changing the sections that are indicated by the comments:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Find a file in a director and return "true" if found

    I did a combination of Mumps1 and GeneralDisarray, here is my final code:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Check next row if value is, "FALSE," if "TRUE," return specified value from this row
    By Dave_Doodle in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 01-19-2015, 10:18 AM
  2. Replies: 3
    Last Post: 04-14-2013, 11:53 PM
  3. Macro find value in access and return legend "found" without limited range
    By feroguz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-14-2012, 06:27 PM
  4. [SOLVED] Find multiple "text" criteria and return as ""Yes" in Matrix
    By bertrand82 in forum Excel General
    Replies: 11
    Last Post: 04-30-2012, 09:20 AM
  5. VLookup to return "true" or "false"
    By Sph01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2011, 11:10 AM
  6. Using "Find" to return results if a word is found within multiple words in a cell
    By pylauzier in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-01-2011, 01:37 PM
  7. Replies: 5
    Last Post: 10-12-2010, 06:46 AM

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