There will be times when you want to remove a particular macro from a document (or template) and there are a few ways you can do this:
1) Using the Macro dialog box
2) Using the Visual Basic Editor
Using the Macro dialog box
You can delete a macro using the Macro dialog box. Select (Tools > Macro > Macros).
Select the name of the macro you want to delete from the list and press the "Delete" button.
This will remove the macro from its corresponding code module.
Using this method will not remove any empty code modules from your VBA Project.
Using the Visual Basic Editor
You can also delete macros manually by displaying the Visual Basic Editor.
This window can be displayed by selecting (Tools > Macro > Visual Basic Editor) or by pressing (Alt +
F11).
You will need to locate the Project in the Project Explorer window. This should appear in the top left corner. If you cannot see it, select (View > Project Explorer).
There should be a project there called VBAProject followed by the name of the document in brackets. Expand this project.
Select the Modules node and expand it. This will list all the code modules that are contained within this document.
You can double click on any module to display the corresponding
code window.
Once you have located the macro in question, you can highlight it with the mouse and press the Delete key.
Deleting all the Macros
If you want to delete all the macros from a document then it is much easier to do this using the Visual Basic Editor.
This way you can use the shortcut menu to quickly remove a code module from the project.
Select the code
module you want to delete and press the right mouse button and select "Remove Module".
You will be asked if you want to export the module first. Select "No".
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions LimitedTopPrevNext
| ||
Excel | Remove All VBA Code Programmatically | |
Ease of Use | Intermediate | |
Version tested with | 2002 | |
Submitted by: | Jacob Hilderbrand | |
Description: | A method to use VBA code to remove all VBA code in a workbook. | |
Discussion: | You have a lot of modules, userforms, and other code in a workbook and you want to delete it all at once. You could manually delete each one, one at a time, or you can use VBA to do all the work for you. | |
Code: | instructions for use | |
Option Explicit Sub DeleteAllCode() Dim x As Integer Dim Proceed As VbMsgBoxResult Dim Prompt As String Dim Title As String Prompt = "Are you certain that you want to delete all the VBA Code from " & _ ActiveWorkbook.Name & "?" Title = "Verify Procedure" Proceed = MsgBox(Prompt, vbYesNo + vbQuestion, Title) If Proceed = vbNo Then MsgBox "Procedure Canceled", vbInformation, "Procedure Aborted" Exit Sub End If On Error Resume Next With ActiveWorkbook.VBProject For x = .VBComponents.Count To 1 Step -1 .VBComponents.Remove .VBComponents(x) Next x For x = .VBComponents.Count To 1 Step -1 .VBComponents(x).CodeModule.DeleteLines _ 1, .VBComponents(x).CodeModule.CountOfLines Next x End With On Error GoTo 0 End Sub | ||
How to use: |
| |
Test the code: |
| |
Sample File: | DeleteAllCode.zip 7.81KB | |
Approved by mdmackillop | ||
This entry has been viewed 368 times. | ||