How to Write a Workbook Self-Deletion Code When a Specific Date is reached?

Wulf

Active Member
Joined
Dec 1, 2004
Messages
395
Office Version
  1. 365
Platform
  1. Windows
You design, build, and complete a Workbook.

But you want this Workbook to self-delete itself when a certain date is reached.

IE: You sell it, it's good for one year, and the ones who bought have the option to renew it at the end of that year. They choose NOT to, so when that time comes around, you need it to self-delete itself.

How do I write that VBA code?

Thank you.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Put somewhere is your code
Code:
if date1>=date2 then Call SuicideSub
Code:
Sub SuicideSub()
' Original code from Tom Urtis
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
Application.Quit
End With
End Sub
 
Upvote 0
Put somewhere is your code
Code:
if date1>=date2 then Call SuicideSub
Code:
Sub SuicideSub()
' Original code from Tom Urtis
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
Application.Quit
End With
End Sub


The line "if date1>=date2"... I have to assume that these are reference dates for the code to trigger.
How/where do I edit these?

Thank you.
 
Upvote 0
After and on July 1 this year, the code would not run past the first line because the current date would be greater than the criteria date.

Code:

VBA Code:
Sub t()
If Date >= #7/1/2019# Then Exit Sub 'change this from Exit Sub to Kill and the filename
'Your regular code here
End Sub

Date is current date
The date with the pound symbols is the criteria date. The pound symbols make it date literal so it will be the correct data type.

You auto run the above macro with a reference in the ThisWorkbook_Open module :

VBA Code:
Private Sub ThisWorkbook_Open()
     Call t
End Sub
 
Upvote 0
I can't get it to work.

I eventually ended up with this:
Private Sub ThisWorkbook_Open()
If Date >= #8/7/2020# Then Call SuicideSub
Sub SuicideSub()
' Original code from Tom Urtis
With ThisWorkbook
.Saved = True
Kill .FullName
Application.Quit
End With
End Sub

Which does nothing, but when I had your code as typed, it kept telling me that there was an error and would send me to the Debugger.

The code below does nothing, either.
If Date >= #8/7/2020# Then Call SuicideSub
Sub SuicideSub()
' Original code from Tom Urtis
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
Application.Quit

I can't get anything to execute the desired effect.
 
Upvote 0
.
In the THISWORKBOOK MODULE .. paste this :

VBA Code:
Option Explicit

Private Sub Workbook_Open()
        SuicideSub
End Sub


Then in a REGULAR MODULE .. paste this :

VBA Code:
Option Explicit

Sub SuicideSub()
    If Date >= #8/6/2020# Then
        ActiveWorkbook.Saved = True
        Application.ActiveWorkbook.ChangeFileAccess xlReadOnly
        Kill Application.ActiveWorkbook.FullName
    End If
    Application.Quit
End Sub

Then save the workbook. Next time you open the workbook it will self-destruct. No reversal / no second chances to re-consider.
 
Upvote 0
1596831622428.png


1596831702769.png
 
Upvote 0
ThisWorkbook - Workbook Open.jpg


The above image shows where the macro Workbook_Open should be located. Look on the left and you will find THIS WORKBOOK. Double click on that, then in the large
white window that shows on the right ... paste the macro as shown above.

Next, from the menu bar up above, click on INSERT. Click on MODULE. In the large white window on the right, paste the following macro :

Regular Module.jpg
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top