
Tips.Net > ExcelTips Home > Macros > Creating Macros > Self-Deleting Macros
Summary: Macros eventually lose their purpose and are no longer needed. There can be any number of reasons for them no longer being needed, such as a particular date being passed. If you want to delete macros after a particular date is passed, then the information in this tip will be of particular interest to you. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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 = #2/1/2007#
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 February 1, 2007, then the main body of the macro will execute. If it is February 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 >= #2/1/2007# 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.htm
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 Feburary 1, 2007, then the 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.
Tip #3123 applies to Microsoft Excel versions: 97 2000 2002 2003
PivotTables Got You Perplexed? ExcelTips: PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of the PivotTable Wizard, how to edit PivotTables, how to format them, how to customize them, and much more.
Check out ExcelTips: PivotTables for the Faint of Heart today!
No, not that type of date. If you need to do any types of work with calendar dates, Excel has the tools you need. Learn how to use those tools the easy way. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site