+ Reply to Thread
Results 1 to 11 of 11

Look for a File and create one if it doesn't exist

  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Look for a File and create one if it doesn't exist

    Ive been working on a bit of VBA code for a while now but I cant seem to get it to work.
    What i want tit to do is to look for a file and create one if it doesnt exist but at the same time for it to be able to be used in any file location.
    this is what ive got so far:

    Please Login or Register  to view this content.
    Any help would be much appreciated
    Thanks

    Moderator's Note: You have to put code tags around your codes.Thank you.
    Last edited by Titansmasher; 11-09-2012 at 04:21 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Look for a File and create one if it doesn't exist

    Not entirely sure what you want to be able to do. What doesn't your routine do that you want it to do?


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Look for a File and create one if it doesn't exist

    i want it to search for a document, which name is defined in cell C3, and if it doesn't exist then to create one.
    The problem is is that the sub findfile isn't finding the document and so is over righting the existing document and I am loosing the data inside it.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Look for a File and create one if it doesn't exist

    So, what's actually in cell C3?. You need to step through the code and see what file name is built and checked for. If it doesn't match the file name in the folder, that's your problem.

    Bear in mind that only you know what data is in the cell(s).


    Regards, TMS

  5. #5
    Registered User
    Join Date
    11-08-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Look for a File and create one if it doesn't exist

    Cell C3 has the current year and i want it to search for a document with the name Register Cell c3.
    I ran the naming part of the code on its own; ThisWorkbook.Path & "\Registers\Register " & Range("C3"), and it gave the correct file location and name but the code still isn't working

    Thanks

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Look for a File and create one if it doesn't exist

    Suggest you post a sample workbook with typical data and code.

    Regards, TMS

  7. #7
    Registered User
    Join Date
    11-08-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Look for a File and create one if it doesn't exist

    I want the code to search and create a file called "register 2013" with 2013 coming from cell C3 and then to create an individual sheet for each other value in column C.
    The problem is that I cant seem to get it to look for a file depending on "Summer Camp register editor.1.xlsm"'s current location; using ThisWorkbook.Path, so that the folder it is in can be moved
    around but it will still be able to find the file "register 2013"

    Thanks for your Patience Trevor
    Attached Files Attached Files
    Last edited by Titansmasher; 11-08-2012 at 04:08 PM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Look for a File and create one if it doesn't exist

    Stepping through your code ...

    What you search for with Dir:

    Please Login or Register  to view this content.

    and what you then create when you find that doesn't exist:

    Please Login or Register  to view this content.

    Spot the difference?


    I'm guessing that you don't have Windows Explorer configured to show file extensions. If a file is shown with an Excel icon, it has been recognised as an Excel file and, for this to happen, it must have an Excel extension. So, just because a file name isn't listed with a file extension, it doesn't mean it isn't there.

    When you are using Dir to check for the existence of the file, add: ' & ".xlsx"' (without the single quotes).


    Regards, TMS
    Last edited by TMS; 11-08-2012 at 05:02 PM.

  9. #9
    Registered User
    Join Date
    11-08-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Look for a File and create one if it doesn't exist

    Thanks for pointing that out.
    I did already know about file extensions but didn't realize that it was required for this.
    Ive managed to get it working now

    Much appreciated Trevor

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,377

    Re: Look for a File and create one if it doesn't exist

    You're welcome. Thanks for the rep.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  11. #11
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Look for a File and create one if it doesn't exist

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as "SOLVED".
    For the meantime I'll do it for you.
    I think people forget the word "THANK YOU!!!!" Do you still know it???

    There is a little star ( ADD REPUTATION ) below those person who helped you. Click it to say your "PRIVATE APPRECIATION TO THEIR EFFORT ON THEIR CONTRIBUTIONS "

    Regards,
    Vladimir

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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