+ Reply to Thread
Results 1 to 18 of 18

Using FileExist to check name before saving file

  1. #1
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Post Using FileExist to check name before saving file

    We use a very sophisticated Excel Spreadsheet to generate Order Confirmations, Invoicing and Delivery Notes. It is full of Macros so everything is automated and it takes out the human error but the only thing we can't control is saving the file as the wrong number so looking for a workround.

    Typically we save files as:

    'OC6027.pdf', 'OC6028.pdf', 'OC6029.pdf'... or 'IN6027.pdf', 'IN6028.pdf', 'IN6029.pdf'...

    but nothing stopping the user accidentally putting 'OC6020' in the first spreadsheet as the next sequential number and then the current macros will copy across the error to the rest of the paperwork and just overwrite an existing file.

    Is there a way to look into a folder (we only use 1 folder on the entire network) to check the last sequential file name and return it's value +1?

    The only other alternative would possibly be to use If FileExist in the current Macro before saving

    Can anybody give any guidelines?
    Last edited by pvking; 07-16-2019 at 06:11 AM. Reason: Making title more relevant

  2. #2
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: How to look up a filename and return the next Sequential number

    Take a look at this thread, see you can work with it;

    https://www.excelforum.com/excel-pro...ml#post5143545

    Post back if you need more help

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: How to look up a filename and return the next Sequential number

    try these two procedures, that should work for you:

    Please Login or Register  to view this content.
    change what is in RED to meet your needs
    Last edited by dmcgov; 07-11-2019 at 09:09 AM. Reason: commented out a debug.print line

  4. #4
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to look up a filename and return the next Sequential number

    Hi DMCGOV and PaulSP8 and thanks for the assistance although after a few hours of trying to get this to work, have decided that this might be too complicated for what we need.

    After a bit of a re-think, all we need to do is ensure that the file is saved as what is in the Range("G3") which is working well and just ensure we don't overwrite another file so going down the MsgBox route.

    So all we need is to check if the value Range("G3") does not exists as a file name in the folder and if so bring up a vbYesNo message box which lets the user know the file exists and give the option to overwrite or abort. This is where I am at the moment but getting a 'Compile Error' on the Range("G3") saying Sub or Function not defined even though it is working perfectly well later in the routine.


    Please Login or Register  to view this content.

    Have I got the FileExist wrong?
    Last edited by AliGW; 07-15-2019 at 07:22 AM.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,430

    Re: How to look up a filename and return the next Sequential number

    Administrative Note:

    We would very much like to help you with your query, however the thread title does not really convey what your request is about.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. You are still quite new, so I will do it for you this time.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  6. #6
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: How to look up a filename and return the next Sequential number

    can you post the 'FileExist' function? and maybe post the workbook too.

  7. #7
    Registered User
    Join Date
    07-15-2019
    Location
    Mechelen, Belgium
    MS-Off Ver
    Office 365
    Posts
    16

    Re: How to look up a filename and return the next Sequential number

    Wouldn't it be an option to rather block the user from changing the sequential number?
    You could even just use a data validation on "Cell R[-1]C (row-1 on same column) + 1", that way you take out the error at the root rather then creating a complex code for what seems to be an easy problem...

    Could be I get your problem wrong, but I guess the data validation could work, no?

    Worst case you could even just put block the entire column and just add a formula that wil calculate the "invoice number" or whatever it is you need as the filename?

    Regards,
    Rob

  8. #8
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to look up a filename and return the next Sequential number

    Hi RobVerheyden that is an interesting option and didn't think of that. The files currently look like:

    OC2213.pdf
    OC2214.pdf
    OC2215.pdf
    OC2216.pdf


    etc etc and generally the user looks up the last number in the folder and types in the next one. They should enter "OC2217" but have in the past types something like "OC2117" and overwritten files which we can't detect or retrieve.

    The most ideal situation would be a macro button beside the which they press and it automatically works out the last file that exists and enters the value +1 then they don't even have to look in the folder.

    Thanks for thinking outside the box
    Last edited by pvking; 07-16-2019 at 05:59 AM. Reason: removing colours

  9. #9
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to look up a filename and return the next Sequential number

    Hi again dmcgov

    below if the full file FileExist that isn't working in red.
    Please Login or Register  to view this content.
    I am not sure if we have got the arguments correct or if pressing the "NO" button will jump to the end and miss out the file saving and printing.

    at the moment we are using everything else

    Thanks for your support
    Last edited by pvking; 07-16-2019 at 06:15 AM. Reason: removing colours

  10. #10
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to look up a filename and return the next Sequential number

    hi colours taken out and modified the title to be more relevant - sorry still understanding the etiquette

    What are the code tags?
    Last edited by pvking; 07-16-2019 at 06:13 AM.

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,430

    Re: Using FileExist to check name before saving file

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. I will do it for you this time - next time, you must do this yourself, please.)

    You really need to read our forum rules, please:
    https://www.excelforum.com/forum-rul...rum-rules.html
    Last edited by AliGW; 07-16-2019 at 06:16 AM.

  12. #12
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: Using FileExist to check name before saving file

    That's great help and done it

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,430

    Re: Using FileExist to check name before saving file

    I did it for you.

    Please take a moment to read those rules - they aren't complicated.

  14. #14
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: Using FileExist to check name before saving file

    Yes already had a scan through. Thanks again for your help

  15. #15
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Using FileExist to check name before saving file

    please post a desensitized workbook. there are issues with fileexist and variables, but i need to see what your seeing.

    actually do this:

    Please Login or Register  to view this content.
    Last edited by dmcgov; 07-16-2019 at 07:12 AM.

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Using FileExist to check name before saving file

    Based on the filenames you provided in Post#8.

    This function will lookup last used number and add 1.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  17. #17
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: Using FileExist to check name before saving file

    Hi dmcgov I am happy to share so find the file attached.

    There are a lot of Vlookups referring to customer lists and price lists but have taken out any personal information and the turned off the cell protection

    I have also had to delete a lot of the pages like Purchase Orders, Delivery Notes and Pro-Forma which are all similar but needed to get under the 1Mb

    Hope it makes sense
    Attached Files Attached Files

  18. #18
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: Using FileExist to check name before saving file

    so change this:

    Please Login or Register  to view this content.
    to this:
    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. Replies: 3
    Last Post: 02-03-2019, 04:22 PM
  2. [SOLVED] Return Sequential Numbers Based on Unique Values
    By HangMan in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-06-2015, 08:13 PM
  3. Formula to return filename in folder based on partial filename
    By Rerock in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-26-2014, 12:28 PM
  4. Replies: 10
    Last Post: 10-30-2013, 07:29 PM
  5. find and return sequential numbers
    By twisty in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-27-2013, 10:43 AM
  6. can excel return sequential numbers in an IF statement?
    By aldermju in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-28-2008, 04:16 AM
  7. [SOLVED] ActiveWorkbook.SaveAs FileName;= 'sequential
    By ufo_pilot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2006, 06:40 AM

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