How To Delete all macros in a workbook/document using VBA in Excel?
Tags: todeleteallmacrosina, vbamacros
Published: 2021-10-19 04:44:20

---
title: How To Delete all macros in a workbook/document using VBA in Excel?
published: true
date: 2021-10-16 17:56:48 UTC
tags: ToDeleteAllMacrosInA,VBAMacros
canonical_url: https://geekexcel.com/how-to-delete-all-macros-in-a-workbook-document-using-vba-in-excel/
---

In this tutorial, we will guide you on how to  **delete all macros in a Workbook/Document ** using VBA in Microsoft Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```

Sub RemoveAllMacros(objDocument As Object)
' deletes all VBProject components from objDocument
' removes the code from built-in components that can't be deleted
' use like this: RemoveAllMacros ActiveWorkbook ' in Excel
' or like this: RemoveAllMacros ActiveWorkbookDocument ' in Word
' requires a reference to the 
' Microsoft Visual Basic for Applications Extensibility library
Dim i As Long, l As Long
    If objDocument Is Nothing Then Exit Sub
    i = 0
    On Error Resume Next
    i = objDocument.VBProject.VBComponents.Count
    On Error GoTo 0
    If i < 1 Then ' no VBComponents or protected VBProject
        MsgBox "The VBProject in " & objDocument.Name & _ 
            " is protected or has no components!", _
            vbInformation, "Remove All Macros"
        Exit Sub
    End If
    With objDocument.VBProject
        For i = .VBComponents.Count To 1 Step -1
            On Error Resume Next
            .VBComponents.Remove .VBComponents(i) 
            ' delete the component
            On Error GoTo 0
        Next i
    End With
    With objDocument.VBProject
        For i = .VBComponents.Count To 1 Step -1
            l = 1
            On Error Resume Next
            l = .VBComponents(i).CodeModule.CountOfLines
            .VBComponents(i).CodeModule.DeleteLines 1, l 
            ' clear lines
            On Error GoTo 0
        Next i
    End With
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Save-the-Code-30.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Now, you need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Finally, after executing the macro, it will **delete all macros in a Workbook/Document**.

## A Brief Summary

We hope that this tutorial gives you guidelines on how to  **delete all macros in a Workbook/Document ** using VBA in Microsoft Excel. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Related Articles:**

- **[How to Delete Sheet in Microsoft Excel 365?](https://geekexcel.com/how-to-delete-sheet-in-microsoft-excel-365/)**
- **[Highlight Unique Values in Selection using Macros in Excel 365!!](https://geekexcel.com/highlight-unique-values-in-selection-using-macros-in-excel-365/)**
- **[Setting Chart Position using Macros (VBA) in Excel Office 365!](https://geekexcel.com/setting-chart-position-using-macros-vba-in-excel-office-365/)**
- **[Quick Shortcut to Delete Cells in Microsoft Excel 365!!!](https://geekexcel.com/quick-shortcut-to-delete-cells-in-microsoft-excel-365/)**
- **[Shortcut to Delete Comment in Microsoft Excel 365](https://geekexcel.com/shortcut-to-delete-comment-in-microsoft-excel-365/)**