Create a macro to delete modules, userforms,

Solved
yao.chris Posted messages 95 Status Member -  
yao.chris Posted messages 95 Status Member -
Hello,

do you know how to create a macro that allows you to delete modules, userforms, ThisWorkbook code, and sheet code?
I have tried the codes I found online but none of them work.

Thank you for your help.
yao

5 answers

  1. Mytå Posted messages 4246 Registration date   Status Contributor Last intervention   958
     
    Hello Forum

    Have you added the reference
    "Microsoft Visual Basic For Application Extensibility x.x"?

    Mytå

    --
    Thank you for following up on your question, we are not robots...
    “If debugging is the art of removing bugs, then programming must be the art of creating them.”
    0
  2. yao.chris Posted messages 95 Status Member 2
     
    Good evening Myta,

    Thank you for your response.
    Yes, I did it but with no results.
    Is there a way to create a macro without adding this reference?

    Thank you for your help.
    yao
    0
  3. Mytå Posted messages 4246 Registration date   Status Contributor Last intervention   958
     
    Hello Forum

    To delete code modules.
     Sub DeleteModule() 'Macro to delete code modules from the workbook Dim i As Integer For i = ThisWorkbook.VBProject.VBComponents.Count To 1 Step -1 If ThisWorkbook.VBProject.VBComponents.Item(i).Type = 1 Then ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents.Item(i) End If Next i End Sub 

    In the macro security section, there is a tab called "Trusted Publishers".
    In this tab, you need to check "Trust access to the VBA project object model".

    Mytå
    --
    Thank you for following up on your question, we are not robots...
    “If debugging is the art of removing bugs, then programming must be the art of introducing them.”
    0
    1. Mytå Posted messages 4246 Registration date   Status Contributor Last intervention   958
       
      Re the Forum

      Additional Information
       Type 1: Standard Module
      Type 2: Class Module
      Type 3: Userform
      Type 100: Sheet Module or ThisWorkbook
      Mytå
      0
  4. yao.chris Posted messages 95 Status Member 2
     
    Good evening Mytå,

    I just applied your macro.
    Everything works great except for Type 100; I have a runtime error on Focus.

    The most important thing for me was to remove the Userforms and modules.
    Types 1 and 3 work perfectly.

    Thank you very much for your help.

    Good night.
    yao
    0
    1. Mytå Posted messages 4246 Registration date   Status Contributor Last intervention   958
       
      Re the Forum

      To delete all VBA modules from the file.
       Sub DeleteAllCodeInModule() Dim VBCodeMod As Object Dim I As Long With ThisWorkbook.VBProject For I = 1 To .VBComponents.Count Set VBCodeMod = .VBComponents(I).CodeModule VBCodeMod.DeleteLines 1, VBCodeMod.CountOfLines Next I End With End Sub

      Mytå
      0
  5. yao.chris Posted messages 95 Status Member 2
     
    Hello Mita,

    Well done!!!
    This is exactly what I was looking for, and it works great.

    Thank you for your valuable help.

    Have a great day and thanks again.
    yao
    0