+ Reply to Thread
Results 1 to 3 of 3

Macro to delete itself

Hybrid View

Adam Schaefer Macro to delete itself 01-03-2024, 12:26 PM
JEC. Re: Macro to delete itself 01-03-2024, 01:29 PM
Greg M Re: Macro to delete itself 01-03-2024, 02:12 PM
  1. #1
    Registered User
    Join Date
    10-17-2013
    Location
    San Diego
    MS-Off Ver
    Excel 2016
    Posts
    99

    Macro to delete itself

    Is it possible to use VBA to have a Macro delete itself once finished running?

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Macro to delete itself

    Save the file as xlsx and it's gone

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Macro to delete itself

    Hi there,

    See if the code in the attached workbook does what you need. It contains two methods - one deletes just the routine itself, the other deletes the entire CodeModule which contains the routine.

    The first VBA CodeModule contains the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub RunAndDeleteCodeModule()
    
        Const sMODULE_NAME As String = "Module1"
    
        MsgBox "The VBA CodeModule which contains this routine will now be deleted"
    
        With ThisWorkbook.VBProject
            .vbComponents.Remove .vbComponents(sMODULE_NAME)
        End With
    
    End Sub

    The second VBA CodeModule contains the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub DummyRoutine_1()
    
    '   This routine will remain after the specified routine has been deleted
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub RunAndDeleteRoutine()
    
        Const iVBEXT_PK_PROC    As Integer = 0
        Const sROUTINE_NAME     As String = "RunAndDeleteRoutine"
        Const sMODULE_NAME      As String = "Module2"
    
        Dim objCodeModule       As Object
        Dim iFirstLineNo        As Integer
        Dim iNoOfLines          As Integer
    
        MsgBox "The VB routine which generated this message will now be deleted"
    
        Set objCodeModule = ThisWorkbook.VBProject.vbComponents(sMODULE_NAME).CodeModule
    
        With objCodeModule
    
            iFirstLineNo = objCodeModule.ProcStartLine(sROUTINE_NAME, iVBEXT_PK_PROC)
    
            iNoOfLines = objCodeModule.ProcCountLines(sROUTINE_NAME, iVBEXT_PK_PROC)
    
            .DeleteLines StartLine:=iFirstLineNo, Count:=iNoOfLines
    
        End With
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub DummyRoutine_2()
    
    '   This routine will remain after the specified routine has been deleted
    
    End Sub
    The highlighted values may be altered to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Delete Delete headings and 9 rows above from row 15 onwards
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2021, 12:06 AM
  2. Run Macro Automatically When delete row , changes cell,delete column
    By elevisse in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2012, 07:42 AM
  3. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  4. Replies: 2
    Last Post: 09-13-2005, 12:05 AM
  5. Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  6. Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  7. Re: Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1