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
 
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
Thank you, that helps for a lot of my other code as well.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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