Consulting

Results 1 to 8 of 8

Thread: Solved: Remove all modules.

  1. #1
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location

    Question Solved: Remove all modules.

    What is the VBA code to remove all modules in a workbook?.

    Thanks,

    Marcster.

  2. #2
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    Just having a look around in the object browser
    Added Microsoft Visual Basic for Applications Extensibilty 5.

    Marcster.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Dim oVBComp As Object
    Dim oVBComps As Object

    Set oVBComps = ActiveWorkbook.VBProject.VBComponents

    For Each oVBComp In oVBComps
    Select Case oVBComp.Type
    Case 1, 3, 2
    oVBComps.Remove oVBComp
    Case Else
    With oVBComp.CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    End Select
    Next oVBComp
    [/vba]

  4. #4
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    How to amend the following so it deletes all VBA code in workbook
    except Module3?.

    [VBA]

    Sub RemoveAllVBACode()
    'Remove all VBA code in all modules:

    Dim oVBComp As Object
    Dim oVBComps As Object

    Set oVBComps = ActiveWorkbook.VBProject.VBComponents

    For Each oVBComp In oVBComps
    Select Case oVBComp.Type
    Case 1, 3, 2
    oVBComps.Remove oVBComp
    Case Else
    With oVBComp.CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    End Select
    Next oVBComp


    End Sub[/VBA]

    Thanks,

    Marcster.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Sub RemoveAllVBACode()
    'Remove all VBA code in all modules:

    Dim oVBComp As Object
    Dim oVBComps As Object

    Set oVBComps = ActiveWorkbook.VBProject.VBComponents

    For Each oVBComp In oVBComps
    Select Case oVBComp.Type
    Case 1, 3, 2
    If oVBComp.Name <> "Module3" Then
    oVBComps.Remove oVBComp
    End If
    Case Else
    With oVBComp.CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    End Select
    Next oVBComp

    End Sub
    [/vba]

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I thought it was the component Item(index).Name?? Well, here is another way ...

    [vba]Sub DeleteAllModules()
    Dim oVBComp As Object
    Dim oVBComps As Object
    Dim i As Long
    Set oVBComps = ActiveWorkbook.VBProject.VBComponents
    i = 1
    For i = oVBComps.Count To 1 Step -1
    Set oVBComp = oVBComps.Item(i)
    Select Case oVBComp.Type
    Case 1, 3, 2
    If oVBComps.Item(i).Name = "Module3" Then GoTo SkipComp
    oVBComps.Remove oVBComp
    Case Else
    With oVBComp.CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    End Select
    SkipComp:
    Next i
    End Sub[/vba]

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    I thought it was the component Item(index).Name?? Well, here is another way ...
    Iterating through the components as originally shown addresses the same component as indexing the item. So it is looking at the same item, just using a different addressing method.

  8. #8
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    Thanks guys,

    Marcster.

Posting Permissions

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