Consulting

Results 1 to 2 of 2

Thread: Completely remove form from Excel file

  1. #1

    Completely remove form from Excel file

    I need to completely free an Excel file from all VB-code & objects. The code I found and reworked works fine, but I want to delete the form where a button initiates the code (it starts with myForm.Hide).

    Anyone an idea?

    The code I use:

    Dim mSheet As Object, shtName As String
    ActiveSheet.Shapes("CommandButton1").Delete
    For Each mSheet In Sheets
    shtName = mSheet.CodeName
    With ThisWorkbook.VBProject.VBComponents(shtName).CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    Next
    With ThisWorkbook.VBProject.VBComponents("myForm").CodeModule
    .DeleteLines 1, .CountOfLines
    End With

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    You mean remove all VBA code and all objects? use...
    [vba]Option Explicit
    '
    Sub DeleteAllObjects()
    '
    Dim Sheet As Worksheet
    '
    For Each Sheet In ThisWorkbook.Sheets
    With Sheet
    .Activate
    .Shapes.SelectAll
    Selection.Cut
    End With
    Next
    '
    Call AddReference
    '
    End Sub
    '
    Private Sub AddReference()
    Dim Reference As Object
    With ThisWorkbook.VBProject
    For Each Reference In .References
    If Reference.Description Like "Microsoft Visual Basic for Applications Extensibility*" Then GoSub Finish
    Next
    .References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 0, 0
    End With
    '
    Finish:
    Call DeleteAllVBAcode
    '
    End Sub
    '
    Private Sub DeleteAllVBAcode()
    '
    Dim Component As VBIDE.VBComponent
    '
    For Each Component In ThisWorkbook.VBProject.VBComponents
    '
    Select Case Component.Type
    Case vbext_ct_StdModule, vbext_ct_MSForm, _
    vbext_ct_ClassModule
    ThisWorkbook.VBProject.VBComponents.Remove Component
    Case Else
    With Component.CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    End Select
    Next
    '
    Call RemoveReference
    '
    End Sub
    '
    Private Sub RemoveReference()
    '
    Dim Reference As Object
    '
    For Each Reference In ThisWorkbook.VBProject.References
    If Reference.Description Like "Microsoft Visual Basic for Applications Extensibility*" Then
    ThisWorkbook.VBProject.References.Remove Reference
    End If
    Next
    '
    End Sub[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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