Consulting

Results 1 to 3 of 3

Thread: Sleeper: Delete a macro

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    Sleeper: Delete a macro

    hello
    the following macro run a specific macro from a list
    on a worksheet.
    how can i add a msgbox to tell the user he chose a wrong name.
    secondly , is ther a way to delete a macro from the vba
    editor.namely if i can run it can i delete it.
    thanks

    Sub testers2() 
    On Error GoTo canceled:
    Dim y As String
    y = InputBox("select macro to run ", "a macro runner")
    With Application
    .Run y
    End With
    canceled:
    End Sub
    moshe

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Part 1
    Sub testers1()
    On Error GoTo canceled:
    Dim y As String
    y = InputBox("select macro to run ", "a macro runner")
    On Error GoTo ErrH
    Application.Run y
    exit sub
    ErrH:
    MsgBox "Macro " & y & " not found."
    canceled:
    End Sub
    Part 2 (a bit more involved)
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=250
    Regards,
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    is ther a way to delete a macro from the vba
    editor.namely if i can run it can i delete it.


    Const vbext_pk_Proc = 0
    
    Sub DeleteProcedure()
    Dim oCodeModule As Object
    Dim iStart As Long
    Dim cLines As Long
    Set oCodeModule = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
        With oCodeModule
            On Error GoTo dp_err:
            iStart = .ProcStartLine("myProc", vbext_pk_Proc)
            cLines = .ProcCountLines("myProc", vbext_pk_Proc)
            .DeleteLines iStart, cLines
            On Error GoTo 0
            Exit Sub
        End With
    dp_err:
        If Err.Number = 35 Then
            MsgBox "Procedure does not exist"
        End If
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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