+ Reply to Thread
Results 1 to 8 of 8

How to enable user to click "cancel" if file name exists when saving as...

Hybrid View

  1. #1
    Registered User
    Join Date
    02-28-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    How to enable user to click "cancel" if file name exists when saving as...

    I have a command button that saves my workbook with a new name generated from two different cells. I have looked over many threads and can't quite figure out how to solve my problem: when a user enters a name that already exists and clicks the button, it tells them that the file name exists and they can choose whether or not to replace it. If they click "cancel" or "no" then they receive an error. Can someone help me find a way to enable them to choose no or cancel without getting this error?

    Here is the code I have so far:


    Private Sub CommandButton1_Click()
           
        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & Range("L4") & " " & Range("N4"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
        Range("A8:F308").Select
        Selection.ClearContents
    
    ThisWorkbook.Save
        
    End Sub
    It works wonderfully except for when the user accidentally saves it when the file name already exists and they choose to cancel the action. Any help would be appreciated!

    Thanks in advance
    Last edited by TBO; 01-27-2014 at 04:01 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How to enable user to click "cancel" if file name exists when saving as...

    If I understand well

    Private Sub CommandButton1_Click()
        Application.DisplayAlerts = False       
        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & Range("L4") & " " & Range("N4"),_ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        Application.DisplayAlerts = True
        Range("A8:F308").Select
        Selection.ClearContents
    
    ThisWorkbook.Save
        
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to enable user to click "cancel" if file name exists when saving as...

    Hi, TBO,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    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



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    02-28-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: How to enable user to click "cancel" if file name exists when saving as...

    Sorry, I am still new to this Forum...

    Yes that code works great, Patel45, as long as they are ok with replacing the existing file. I would still like them to be able to have the option to click the cancel button if they didn't realize they were saving over another file.

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How to enable user to click "cancel" if file name exists when saving as...

    Private Sub CommandButton1_Click()
        On Error Resume Next
        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & Range("L4") & " " & Range("N4"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
        Range("A8:F308").Select
        Selection.ClearContents
    
    ThisWorkbook.Save
        
    End Sub

  6. #6
    Registered User
    Join Date
    02-28-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: How to enable user to click "cancel" if file name exists when saving as...

    Thanks, that gave me a head start as to where I needed to go to get what I wanted. Here is the sub routine that ended up working for me. Thanks again!

    Private Sub CommandButton1_Click()
    
        On Error GoTo ErrorHandler
    
        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & Range("L4") & " " & Range("N4"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
        Range("A8:F308").Select
        Selection.ClearContents
    
    ThisWorkbook.Save
    
    ErrorHandler: Exit Sub
        
    End Sub

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to enable user to click "cancel" if file name exists when saving as...

    Hi, TBO,

    could you be kind enough to explain the diference between ActivbeWorkbook and ThisWorkbook to me as you let the active workbook save, alter the active workbook via ClearContents and then save ThisWorkbook (workbook with code)?
    Private Sub CommandButton1_Click()
    
    On Error GoTo ErrorHandler
    
    Range("A8:F308").ClearContents
    
    With ActiveWorkbook
      .SaveAs Filename:=.Path & "\" & Range("L4") & " " & Range("N4"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    End With
    
    ErrorHandler: 'last command in this procedure, procedure will exit automaticly
        
    End Sub
    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    02-28-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: How to enable user to click "cancel" if file name exists when saving as...

    To answer your question, this is a spreadsheet that will be recreated each month and new data will need to be entered and old data erased. So first I save the workbook I am working in to make sure old data in old workbook stays, then I save it as a new workbook for a new month, and then clear the old data from the previous month, then save the workbook again to make sure the old data is gone for good.

    I hope that makes sense. Again, I am super new to VBA and so I am not sure if that is the best way to accomplish that, but it made sense in my head and it works great.

    Thanks!

+ 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. Toggle BackColor of labels in Userform through "click" and "double-click"
    By ahmerjaved in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 02:08 PM
  2. VBA - IE - click on "Open" in an internet explorer "file download" window
    By victoire in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-22-2013, 07:08 AM
  3. Saving a file to a "YYYY" and "MMM" folder
    By hriggs in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-10-2008, 07:00 AM
  4. [SOLVED] Stop code from running when I click "Cancel"
    By Dean in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-01-2006, 08:40 PM
  5. [SOLVED] How do I get "file" tab to click on, it starts with "edit"
    By JMD_Phoenix in forum Excel General
    Replies: 1
    Last Post: 01-22-2006, 10:50 PM

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