+ Reply to Thread
Results 1 to 2 of 2

loop until a file name is entered in a save that does not already exist

  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    Florida USA
    MS-Off Ver
    Excel 2010
    Posts
    9

    loop until a file name is entered in a save that does not already exist

    I have the following code at the back side of a macro to copy some of the contents of a workbook to a new workbook and to save the new workbook with a file name that the user has to enter. I want to prevent the user from overwriting a file with the same file name in a loop until the user enters a unique file name. Here is the code I have so far:

    Set NewBook = Workbooks.Add
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Selection.ColumnWidth = 10.57
    Sheets("Sheet2").Select
    ActiveWindow.SelectedSheets.Delete
    ActiveWindow.SelectedSheets.Delete
    Do
    fname = InputBox("Type in the Reference Number shown at the top left hand portion of your screen now.")
    Loop Until fname <> False
    NewBook.saveas Filename:="\\mrbcapa\CAPA File\CAPA# " & fname
    ActiveWorkbook.Close

    Workbooks("MAF-8.5-0.XLS").Close SaveChanges:=False

    At the NewBook.save as, if the file exists, you get the standard Excel message box that the file already exists and do you wish to overwrite.

    I want the code to disallow this option and simply loop asking for a unique name until the user enters a unique file name.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: loop until a file name is entered in a save that does not already exist

    Maybe this will help.

    http://www.excelguru.ca/content.php?...Folder)-Exists

+ 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