+ Reply to Thread
Results 1 to 3 of 3

VBA command button for saving as date stamped copy in new location (and original)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Question VBA command button for saving as date stamped copy in new location (and original)

    Hi All

    I wonder if anybody could help me with creating a button (already done that bit ;-)) which does the following for a series of workbooks in Excel 2007 (if it could be compatible with 2003 as well that would be great, but not essential):

    The Private Sub CommandButton1_Click() has to do the following:

    - Save a copy of the current workbook to a directory which is the same as the current active workbook filepath plus \Log1 (extra folder)
    - Names the saved file as the current filename plus "_" plus the current date formatted mmddyy
    - Save again, but this time saving over the open file being saved (i.e to its present filename and location) to replace the previous version, using the original filename without date stamp.

    So the idea is that this button saves a copy of the active workbook to a backup folder 'Log1' at CurrentFilePath\Log1 with original filename plus _mmddyy, leaves the open workbook unchanged, and then saves again, but this time overwrites itself to its original and present actual location on the hard disk with the same, unchanged, filename, ie. CurrentFilePath\ActiveWorkBook.Name

    I get the impression this shouldn't be too complex, but my VBA skills are sorely lacking.

    Any help would be hugely appreciated.

    Many thanks

    Daniel

    Here is the best I can come up with so far:

    Private Sub CommandButton1_Click()
    
    Dim Filename As String, Filepath As String
    
    Filename = Replace(ThisWorkbook.Name, ".xls", "")
    Filepath = ThisWorkbook.Path & "\Log1\"
    
    
    NewFileName = Application.GetSaveAsFilename(InitialFileName:=Filepath & Filename & " " & Format(Now(), "mmddyy") & ".xls")
    If NewFileName = "FALSE" Then Exit Sub
    
    ActiveWorkbook.SaveCopyAs Filename:=NewFileName
    
    On Error Resume Next
    
    ActiveWorkbook.SaveAs ThisWorkbook.FullName
    
    End Sub
    How do I get it to completely exit if I cancel at the first prompt when it brings up the 'Save As' dialog? At the moment, if I cancel here, it still prompts me for the overwrite of the original filname afterwards. Ideally I'd like it to skip this if I cancel in the first instance.

    Also, if I want to get rid of the prompt for the overwrite of the original, how do I do this?

    Is there anything else I should have in my code which is missing?

    Many thanks for any help.

    Daniel
    Last edited by pike; 01-08-2012 at 01:16 AM. Reason: set replies to zero

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: VBA command button for saving as date stamped copy in new location (and original)

    first a folder must be opened in the main folder


    then create a command button in the sheet. and if it is not named as commandbutton1 name it like that

    now insert this event code there(right clilck the sheet tab and click view code)

    Private Sub CommandButton1_Click()
    Dim Filename As String, Filepath As String
    Filename = ThisWorkbook.Name
    Filepath = ThisWorkbook.Path & "\Log1\"
    
    '===================================================
    'NewFileName = Application.GetSaveAsFilename(InitialFileName:=Filepath & Filename & " " & Format(Now(), "mmddyy") & ".xls")
    'If NewFileName = "FALSE" Then Exit Sub
    '
    'ActiveWorkbook.SaveCopyAs Filename:=NewFileName
    '
    'On Error Resume Next
    '
    'ActiveWorkbook.SaveAs ThisWorkbook.FullName
    '==========================================================
    ThisWorkbook.SaveAs Filepath & Filename & " " & WorksheetFunction.Text(Date, "mmddyy") & ".xls"
    ThisWorkbook.Save
    End Sub
    Last edited by venkat1926; 01-08-2012 at 03:37 AM.

  3. #3
    Registered User
    Join Date
    06-25-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: VBA command button for saving as date stamped copy in new location (and original)

    Thanks for your reply, but can you explain exactly what you mean by 'first a fodler must be created in the main folder'?

    Sorry if it's hidden in plain view, I miss things like that a lot

    Daniel

+ 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