Microsoft KB Archive/183101

= XL98: How to Delete a Sub Procedure After It Runs Once =

Article ID: 183101

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q183101



SUMMARY
This article contains an example that programmatically deletes a Visual Basic for Applications procedure after you run it once.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following sample macro (Sub procedure) displays a message box, and then deletes the current Visual Basic module. Although the steps to create the sample macro are written for Microsoft Excel, the code can be used in other programs that support the Visual Basic for Applications programming language and the Visual Basic Editor.

NOTE: Because the macro in this example deletes the module that contains it, store procedures that you want to keep in a separate module.

To use this example, follow these steps:  Start Microsoft Excel, and then create a new workbook. Press OPTION+F11 to start the Visual Basic Editor. On the Insert menu, click Module. Press F6 to open the Properties window. In the Properties window, change the (Name) property of the module to TestModule .  Type the following code into the module: Sub DeleteAfterRun

Dim x As Object

' This is the code that I want to run once. MsgBox "Hello, World!"

Set x = Application.VBE.ActiveVBProject.VBComponents

' Delete the current module. x.Remove VBComponent:=x.Item("TestModule")

End Sub  Press OPTION+F11 to switch to Microsoft Excel.</li> Run the DeleteAfterRun macro, and then press ALT+F11 to switch to the Visual Basic Editor.

The TestModule module has been deleted.</li></ol>

<div class="references_section">