Consulting

Results 1 to 10 of 10

Thread: Delete all VBA Modules - Except a Few

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Delete all VBA Modules - Except a Few

    Hi folks,



    I am trying to delete all modules from my workbook except 2 or 3 that I need.

    So I put this code together from research


    Sub DeleteModulesExcept()
    
            Dim VBProj As VBIDE.VBProject
            Dim VBComp As VBIDE.VBComponent
        
            Set VBProj = ActiveWorkbook.VBProject
          
            For Each VBComp In ActiveWorkbook.VBProject.VBComponents
            
            If VBComp.VBProj.VBComponents("DontDelete1") or VBComponents("DontDelete2") Then
            'Dont Delete 
            Else
            VBProj.VBComponents.Remove VBComp
            End If
            Next
        End Sub
    I tried to follow the logic but, well I'm not sure how to make it work

    thank you if you can help or advise

    dj
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    "VBComp.VBProj.VBComponents"

    You are going a bit backwards there. We've already drilled down to the component in the For Each. Warning: air-code, but something along the lines of...
    For Each VBComp in VBProj.VBComponents
      If Not (VBComp.Name = "NotMe" Or VBComp.Name = "MeEither") Then
        VBProj.VBComponents.Remove VBComp
      End If
    Next
    NOT TESTED, so please in a junk copy of your wb...

    Mark

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hi Mark,

    i initially tried the If NOT - but could not get it to work

    I get an error on this line now
    invalid call to procedure

    VBProj.VBComponents.Remove VBComp

    thanks
    dj
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I am signing out dj, but I would suggest attaching a workbook with the code exactly as you have it now.

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hi folks,
    attached file

    dj
    Attached Files Attached Files
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    NB. Put this macro in the codemodule of 'thisWorkbook'

    Sub M_snb()
       ThisWorkbook.VBProject.VBComponents("dont_delete").Export "G:\OF\doont"
    
       With Workbooks.Add
          .VBProject.VBComponents.Import "G:\OF\doont"
       End With
    End Sub
    or

    Sub M_snb()
       For Each it In ThisWorkbook.VBProject.VBComponents
            If it.Type = 1 And it.Name <> "Dont_Delete" Then ThisWorkbook.VBProject.VBComponents.Remove it
       Next
    End Sub
    Last edited by snb; 07-04-2016 at 08:39 AM.

  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you SNB,

    you are a PRO

    I only spent the early morning with so many code lines up to my eyes

    Have a good day my friends

    cheers
    dj
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I had this in a module named 'Dont_Delete'

    1. You need to check the module .Type also

    Option Explicit
    'http://www.cpearson.com/excel/vbe.aspx
    Sub DeleteModulesExcept()
         
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
         
        Set VBProj = ActiveWorkbook.VBProject
         
        For Each VBComp In ActiveWorkbook.VBProject.VBComponents
            Debug.Print "Checking " & VBComp.Name
            If VBComp.Type = vbext_ct_StdModule Then
                If VBComp.Name = "Dont_Delete" Or VBComp.Name = "Module2" Then
                     'Dont Delete
                Else
                    Debug.Print "Removing " & VBComp.Name
                    VBProj.VBComponents.Remove VBComp
                End If
            End If
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you Paul,

    you can never have too many versions of code



    I can use this too
    dj
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645

Posting Permissions

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