+ Reply to Thread
Results 1 to 4 of 4

Delete a file that has just been saved and open original file or vice versa.

  1. #1
    Registered User
    Join Date
    11-17-2015
    Location
    Sheffield, England
    MS-Off Ver
    2016
    Posts
    2

    Delete a file that has just been saved and open original file or vice versa.

    Hi guys,

    In the workbook I am designing I need to be able to save files with a specific name, made up of the data from a specific cell and so I created a button to do so which worked fine.

    Now I also want to give the option to undo this action by creating an undo button that will delete the saved file and open the original document.

    My code is as follows:

    Sub Undosave()

    Application.ScreenUpdating = False
    Application.OnTime Now, "Undosave2"
    ThisWorkbook.Close SaveChanges:=False

    End Sub

    Sub Undosave2()

    Application.Workbooks.Open ("C:\Users\Seth\Documents\Costing Program\Process Costing\Process Costing Updating")
    Windows("C:\Users\Seth\Documents\Costing Program\Process Costing\Process Costing Updating").Activate
    Kill ("C:\Users\Seth\Documents\Costing Program\Process Costing\" & Range("K37").Value)
    MsgBox "Deleted"
    Application.ScreenUpdating = True

    End Sub


    Any help would be gratefully appreciated
    Last edited by SHB19; 11-17-2015 at 06:30 AM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Delete a file that has just been saved and open original file or vice versa.

    Hi, welcome to the forum
    It took me a little time to understand what you've written.
    Is this UndoSave button is on the file you're going kill?

    My idea for this code would be something like this:

    Please Login or Register  to view this content.
    I don't know why to use the Application.OnTime while you can just invoke the macro?
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    11-17-2015
    Location
    Sheffield, England
    MS-Off Ver
    2016
    Posts
    2

    Re: Delete a file that has just been saved and open original file or vice versa.

    Thanks keebellah,

    The UndoSave button is on both files as they are both identical, the only difference being the name of the file.

    I am quite new to VBA so I basically just found the Application.OnTime line online and thought it might work, from what I can gather it allows me to do a process on a file even after it is closed, is this correct?

    So I tried the code you gave me and it seemed to work much better however it came up with an error of 'Permission Denied' and the the debug is as follows:

    Sub Undosave3()
    Dim tWb As Workbook
    Set tWb = ActiveWorkbook
    Application.ScreenUpdating = False
    Application.OnTime Now, "Undosave4"
    tWb.Close SaveChanges:=False

    End Sub

    Sub Undosave4()
    Dim filename As String
    Application.Workbooks.Open ("C:\Users\Seth\Documents\Costing Program\Process Costing\Process Costing Updating")
    'Windows("C:\Users\Seth\Documents\Costing Program\Process Costing\Process Costing Updating").Activate
    filename = Range("K37").Value & ".xlsm"
    Kill ("C:\Users\Seth\Documents\Costing Program\Process Costing\" & filename) <<<<<<<<<<This is the yellow shaded line for the error
    MsgBox "Deleted"
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Delete a file that has just been saved and open original file or vice versa.

    Please place your code in code brackets so that it is displayed as code


    Please Login or Register  to view this content.
    Application.OnTime is useful in some cases but in this case it has no added value.
    I removed that part and just placed the macro

+ 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. [SOLVED] VB to open a file, input on a file and close again, with NO macros/vb saved to file
    By unreal_event_horizon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2012, 11:41 AM
  2. Delete a Macro from the Copied File while Replicating the Original File
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-24-2011, 09:50 AM
  3. File Saved As Read Only? Can't Delete / Rename File
    By Kolin in forum Excel General
    Replies: 3
    Last Post: 11-02-2010, 12:01 PM
  4. Macro to keep original file open, not 'save as' file
    By LonestarGuy in forum Excel General
    Replies: 4
    Last Post: 06-09-2010, 03:53 PM
  5. Saved file size not reduced to original
    By rustleg in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-29-2009, 07:32 PM
  6. Replies: 4
    Last Post: 02-18-2005, 10:06 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