+ Reply to Thread
Results 1 to 13 of 13

Close open workboos with script

Hybrid View

  1. #1
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Close open workboos with script

    I currently use this code to save a backup copy of my workbook.

    Where the open file is "c:\RV Park Files\Daily Report 2013.xslm"

     
    ActiveWorkbook.Save
    
     ' Save a backup
    
    Sub ChangeTrailer(): Dim Pathplus As String, S As String, D As String, Dt As Date: Dt = Date - 1
    If Day(Dt) > 10 And Day(Dt) < 14 Then S = "th" Else _
    S = Mid("thstndrdthththththth", (DatePart("d", Dt) Mod 10) * 2 + 1, 2)
    
    D = Format(Dt, "mmm") & " " & Format(Dt, "d") & S & " " & Format(Dt, "yyyy")
    
    Pathplus = "C:\A Backup Park Files 13\Daily Rpts 13\Daily Report for  " & D & ".xlsm"
    
        ActiveWorkbook.SaveAs Filename:=Pathplus, _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    This works for using today's date in the backup copy.

    However, I often work on several days reports on the same day, and would like a backup copy with the date for that day's work.

    Is it possible to have the "Pathplus = " (refering to a specific cell within the workbook) " ie "Sheet1!c5"

    Where that cell in the workbook will contain the path & Name I want to use. ie "C:\A Backup Park Files 13\Daily Rpts3\Daily Report for Jan 17th.xlsm"

    Also I would like to close the backup copy at this time and reopen the original file. ie "c:\RV Park Files\Daily Report 2013.xslm"

    ready to enter the next day's data.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,946

    Re: Close open workboos with script

    Have you thought of using "SaveCopyAs" rather than "SaveAs"?

    If you use SaveCopyAs, it does what it says on the tin but the the Active Workbook remains open.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    I used excel's help to see how the write the SaveCopyAs.

    Using that, it didn't work.

    Not sure how it should have been written.

    Thank You

  4. #4
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    Ok, after a lot of trial & errors, I got the 'SaveCopyAs" command to work.
    However, I have to change the name of the file each day to get a difference copy for each day.

    I would still like to have the name changed by reference a cell in the workbook I'm saving.

    Thank You

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Close open workboos with script

    Hi,

    Just refer to the cell range's value when you define the filename you intend using in the SaveCopyAs Filename:= instruction

    e.g.

    stFileName = "any string you want " & Range("your_cell").value & "any other text etc..")
    ActiveWorkBook.SaveAs Filename:= stFileName
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  6. #6
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    Yes that worked just find.

    Thank You Very much

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Close open workboos with script

    Try

     
    
        Dim stwb1 As String, wb2 As Workbook, stPathPlus As String
        stwb1 = ThisWorkbook.Name
        stPathPlus = Sheets("Sheet1").Range("C5") ' where "Sheet1" is the sheet TAB name and C5 contains a character string
        ActiveWorkbook.Save    
        ActiveWorkbook.SaveAs Filename:=stPathPlus
        Set wb2 = ThisWorkbook
        Workbooks.Open Filename:=stwb1
        wb2.Close

  8. #8
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    I copied & Pasted the code, it worked through saving the backup as I named it on sheet1 range c5.

    However it then gives me a run-time error 1004

    saying:
    The file could not be accessed. Try one of the following:
    Make sure the specified folder exists.
    Make sure the folder that contains the file is not read only
    Make sure the file name does not contain any of the following charcters: <>?[]:|ir*
    Make sure the file/path name doesn't contain more than 218

    I press the Debug key and it highlights the code line:

    ActiveWorkbook.SaveAs Filename:=stPathPlus
    It did save the file as I named it for a backup, the stops there.

  9. #9
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    I copied & Pasted the code, it worked through saving the backup as I named it on sheet1 range c5.

    However it then gives me a run-time error 1004

    saying:
    The file could not be accessed. Try one of the following:
    Make sure the specified folder exists.
    Make sure the folder that contains the file is not read only
    Make sure the file name does not contain any of the following charcters: <>?[]:|ir*
    Make sure the file/path name doesn't contain more than 218

    I press the Debug key and it highlights the code line:

    [Select Code]

    ActiveWorkbook.SaveAs Filename:=stPathPlus

    It did save the file as I named it for a backup, the stops there.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Close open workboos with script

    Hi kjsconv

    Is it possible to have the "Pathplus = " (refering to a specific cell within the workbook) " ie "Sheet1!c5"
    Probably something like this
    Pathplus = Sheets("Sheet1").Range("C5").Text
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  11. #11
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    Sorry last response was for Richard.
    He also had that line of code.
    However, I'm not a programer and need more to know how to enter the line start to finish.

    Thank You

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Close open workboos with script

    Hi,

    The code I gave you was 'start to finish'. You just need to add the Sub yourname() and End Sub lines

  13. #13
    Registered User
    Join Date
    12-30-2012
    Location
    Nacogdoches, Tx
    MS-Off Ver
    Excel 2015
    Posts
    65

    Re: Close open workboos with script

    Sorry, that response about 'start to finish" was for a someone else.
    My earlier response to you complete code, ie it gave an error message, still applies.

    I ran the code as a stand lone action and still get the same error message.

    I'm not sure how to fix it.

    Thank You

+ 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