+ Reply to Thread
Results 1 to 9 of 9

check if file exists using fso. then save with an incremental number

  1. #1
    Registered User
    Join Date
    11-11-2014
    Location
    lincoln, england
    MS-Off Ver
    2007
    Posts
    7

    check if file exists using fso. then save with an incremental number

    Hi,
    I am trying to save a file with an incremented file number using fso.
    I know how to check for an individual file name but need to add some sort of loop through the folder and save each file. starting a new file each time.

    Data is filled into a form then added to the file then saved as an incremented number.

    Im quite new to vba so think I need treating with kid gloves :0)

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: check if file exists using fso. then save with an incremental number

    But what happens if one of your incremented files previously saved gets deleted or moved, accidentally?

    I would recommend you write a text file into the same directory that explicitly lists what the next number should be, and when the save is done, write the next number into that text file.


    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 11-13-2014 at 09:25 AM. Reason: Corrected code to not have hidden spaces in numfile.txt
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-11-2014
    Location
    lincoln, england
    MS-Off Ver
    2007
    Posts
    7

    Re: check if file exists using fso. then save with an incremental number

    Hi, thanks for the quick reply.

    I see where your going with this but....
    I only need to start from 1 for each customer which has their own folder.
    I have tried using dir before but each time I start a new file the numbers reset and it overwrites the old file.
    that's why I thought using fileexists would be best?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: check if file exists using fso. then save with an incremental number

    Nah. I would use the method described above. The plumbing is minimal and as shown, it would create the NUMfile.txt as needed in each fPATH as you determine that path with your own methods. Your FileExists method can probably be wrangled into working, it's just going to be more plumbing and looping, unnecessary in my opinion. I stand by my suggestion, adapting for your use in each client folder.

  5. #5
    Registered User
    Join Date
    11-11-2014
    Location
    lincoln, england
    MS-Off Ver
    2007
    Posts
    7

    Re: check if file exists using fso. then save with an incremental number

    Hi again.

    You are right.....
    However... when it saves the first file it is fine, but when the next ones are written it adds a space before the number?
    it woud be fine but im importing the files into another programme that doesn't like spaces.


    Thanks again

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: check if file exists using fso. then save with an incremental number

    Try changing the DIM to fNUM As Long

  7. #7
    Registered User
    Join Date
    11-11-2014
    Location
    lincoln, england
    MS-Off Ver
    2007
    Posts
    7

    Re: check if file exists using fso. then save with an incremental number

    Hi,

    Changing to fnum as long returns a 'type missmatch'

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: check if file exists using fso. then save with an incremental number

    No, that didn't work. I've edited the code above with a tweak that will keep those spaces from being written into the file. Sorry about that.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  9. #9
    Registered User
    Join Date
    11-11-2014
    Location
    lincoln, england
    MS-Off Ver
    2007
    Posts
    7

    Re: check if file exists using fso. then save with an incremental number

    Thanks.

    All sorted

+ 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 if file exists and if so save file incremented by 1 e.g. filename(1).xlsm
    By ratdogexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2014, 01:15 AM
  2. check if file exists and opening it based on file date
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-26-2014, 03:45 AM
  3. Check if file exists
    By v2jtb in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-25-2010, 09:22 AM
  4. Check to see if a file exists
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-09-2007, 04:22 AM
  5. check if file exists
    By Curt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2005, 01:10 PM

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