Written by Allen Wyatt (last updated May 25, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
Patrick is writing a macro, and he wants the macro to delete itself after a specific expiration date is reached. There are a couple of ways that this task can be approached. First, you could write a macro that would only function before a specific date, in the following manner:
Sub MyMacro() ExpirationDate = #1/1/2020# If Now() < ExpirationDate Then 'Rest of macro goes here End if End Sub
The idea is that if (in this case) the current date is prior to January 1, 2020, then the main body of the macro will execute. If it is January 1 or later, then the macro will not execute. This approach, of course, does not actually delete the macro; it simply checks to see that the macro is being executed before a certain date.
To actually get rid of the macro code, you need to take a different approach:
Private Sub Workbook_Open() Dim VBComp As VBIDE.VBComponent Dim VBComps As VBIDE.VBComponents 'Delete if Past Date If Date >= #1/1/2020# Then Set VBComps = ActiveWorkbook.VBProject.VBComponents For Each VBComp In VBComps Select Case VBComp.Type Case vbext_ct_StdModule, vbext_ct_MSForm, _ vbext_ct_ClassModule VBComps.Remove VBComp Case Else With VBComp.CodeModule .DeleteLines 1, .CountOfLines End With End Select Next VBComp End If Set VBComps = Nothing Set VBComp = Nothing End Sub
This code was adapted from a macro originally written by Chip Pearson, available on his site at the following address:
http://www.cpearson.com/excel/vbe.aspx
To make the macro work, you'll need to make sure that there is a reference to Microsoft Visual Basic for Applications Extensibility. (You do this by choosing, in the VB Editor, Tools | References and then choosing Microsoft Visual Basic for Applications Extensibility in the available references.)
The macro runs when the workbook is opened and, if the date is greater than or equal to January 1, 2020, then each component of the VBProject is deleted. This means that the macro is very powerful, because it deletes everything, not just a single procedure or module.
There are a couple of things to keep in mind with this macro, of course. First, if the user chooses to not enable macros when the workbook is opened, then this code will never run, and the macro won't be deleted. Second, deleting macros in this way obviously introduces changes to the workbook. That means that when the workbook is closed, the user will be asked if they want to save their changes. If they choose not to, then the deletions will not be saved, and the macro will again run the next time the workbook is opened.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12812) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Self-Deleting Macros.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...
Discover MoreNeed to hide some macros in your workbook? There are three ways you can do it, as covered in this discussion.
Discover MoreWant to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-05-28 04:52:12
Ken Varley
"when the workbook is closed, the user will be asked if they want to save their changes" ........ a RUN_ONCE routine could be added to automatically save the workbook immediately after the code was deleted.
2019-05-25 10:40:28
Aldo Santolla
Nice. Can this be applied to add or modify a macro?
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments