OzGrid

How to use a macro to auto delete file when passed 15 days

< Back to Search results

 Category: [Excel]  Demo Available 

How to use a macro to auto delete file when passed 15 days

 

Requirement:

 

The user has a workbook and is looking for a macro that auto deletes the file when passed 15 days within the workbook.

 

https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/1197148-macro-auto-delete-file-when-passed-15-days

 

Solution:

 

Paste this into the ThisWorkbook module :

Code:
Private Sub Workbook_Open()
 On Error Resume Next
 Dim Edate As Date
 Dim sh As Worksheet
Edate = Format("30/11/2016", "DD/MM/YYYY") '<----- set the delete date here
     If Date > Edate Then
    DeleteActiveWorkbook
End If
End Sub


Pate this into a Routine Module :

Code:
Sub DeleteActiveWorkbook()
Dim xFullName As String
xFullName = Application.ActiveWorkbook.FullName
ActiveWorkbook.Saved = True
Application.ActiveWorkbook.ChangeFileAccess xlReadOnly
Kill xFullName
Application.ActiveWorkbook.Close False
End Sub

The workbook contents will be deleted but the actual workbook will need to be closed manually by the user.

 

Obtained from the OzGrid Help Forum.

Solution provided by Logit.

 

See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets

 

See also:

How to use an input box to enable a range of cells to autofill
How to Auto populate cells with existing values
How to set up an autofill macro
How to use a macro to auto-scroll

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)