Macro to remove macro that removes macros

jerry12302

Active Member
Joined
Apr 18, 2005
Messages
449
Office Version
  1. 2010
Platform
  1. Windows
I have the below VBA code to remove all macros from a workbook, but of course this macro remains.

Is there a way to remove all macros and also remove this macro after it is done, so there are no macros left at all?

Sub remove_macros()

Dim m As Object
Dim mCtr As Integer
Dim oCtr As Variant
Dim vbP As Object
Dim strNames As String
strNames = ThisWorkbook.Name
mCtr = 0

Set vbP = Workbooks("" & strNames).VBProject.VBComponents

For oCtr = 1 To vbP.Count
mCtr = mCtr + 1
If vbP(mCtr).Type = 1 Then
Set m = vbP
m.Remove vbcomponent:=m.Item(m(mCtr).Name)
mCtr = mCtr - 1
End If
Next

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Wouldn't it be easier to just save the file as a .xlsx file?
 
Upvote 0
Because an alert pops up, and if you press continue saving as a macro-free workbook it will save it as an .xlsx file, but macros are still there.

I have a button that runs other code that does a bunch of stuff, then runs the macro to remove macros, no alerts, no popups. But I have to go in afterward and remove that macro too and save the file again.
 
Upvote 0
This will delete all macros in the active workbook :

VBA Code:
Option Explicit

Sub DeleteAllMacros() 'Excel vba to delete all macros in new workbook.
Dim otmp As Object

With ActiveWorkbook.VBProject
    For Each otmp In .VBComponents
        If otmp.Type = 100 Then
            otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
            otmp.CodeModule.CodePane.Window.Close
        Else: .VBComponents.Remove otmp
        End If
    Next otmp
End With
End Sub
 
Upvote 0
Solution
Because an alert pops up, and if you press continue saving as a macro-free workbook it will save it as an .xlsx file, but macros are still there.

I have a button that runs other code that does a bunch of stuff, then runs the macro to remove macros, no alerts, no popups. But I have to go in afterward and remove that macro too and save the file again.
If you save it as .xlsx then close it, there will be no macros when you reopen it. Tested.

VBA Code:
Private Sub savecopynomacros()

   ThisWorkbook.Save ' save any unsaved changes
   ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(ThisWorkbook.Name, ".xlsm", "") & " no macros", FileFormat:=xlOpenXMLWorkbook
   ThisWorkbook.Close
   
End Sub
 
Upvote 0
If you save it as .xlsx then close it, there will be no macros when you reopen it. Tested.

VBA Code:
Private Sub savecopynomacros()

   ThisWorkbook.Save ' save any unsaved changes
   ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(ThisWorkbook.Name, ".xlsm", "") & " no macros", FileFormat:=xlOpenXMLWorkbook
   ThisWorkbook.Close
  
End Sub
 
Upvote 0
I still get the pop up alert, I want to avoid that. With my macro above I don't get any alerts, and it removes all the macros except the macro to remove the macros.
 
Upvote 0
This will delete all macros in the active workbook :

VBA Code:
Option Explicit

Sub DeleteAllMacros() 'Excel vba to delete all macros in new workbook.
Dim otmp As Object

With ActiveWorkbook.VBProject
    For Each otmp In .VBComponents
        If otmp.Type = 100 Then
            otmp.CodeModule.DeleteLines 1, otmp.CodeModule.CountOfLines
            otmp.CodeModule.CodePane.Window.Close
        Else: .VBComponents.Remove otmp
        End If
    Next otmp
End With
End Sub
That did the job exactly, thank you!
 
Upvote 0
I still get the pop up alert, I want to avoid that.
It sounds like you prefer the other solution, but just for completeness, to suppress the alert
Rich (BB code):
Private Sub savecopynomacros()

   ThisWorkbook.Save ' save any unsaved changes
   Application.DisplayAlerts = False
   ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(ThisWorkbook.Name, ".xlsm", "") & " no macros", FileFormat:=xlOpenXMLWorkbook
   Application.DisplayAlerts = True
   ThisWorkbook.Close
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,154
Latest member
pollardxlsm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top