In this blog post, we are going to see how to delete a procedure from a module using VBA in Microsoft Excel. Let’s get into this article!! Get an official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel
Example
- Firstly, you need to create a macro to delete another macro from a module.

- 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.

- Now, you need to copy and paste the code given below.
Option Explicit
Sub DeleteProcedureCode(ByVal DeleteFromModuleName As String, ByVal ProcedureName As String)
'Declaring variables
Dim VBCM As CodeModule, ProcStartLine As Long, ProcLineCount As Long
Dim WB As Workbook
On Error Resume Next
'Creating object of active workbook
Set WB = ActiveWorkbook
'Creating object of workbook module
Set VBCM = WB.VBProject.VBComponents(DeleteFromModuleName).CodeModule
'Checking whether the procedure exist in the codemodule
If Not VBCM Is Nothing Then
ProcStartLine = 0
'Function assigning the line no. of starting line for the procedure
ProcStartLine = VBCM.ProcStartLine(ProcedureName, vbext_pk_Proc)
If ProcStartLine > 0 Then
'Function assign the no. of lines in the procedure
ProcLineCount = VBCM.ProcCountLines(ProcedureName, vbext_pk_Proc)
'Delete all the lines in the procedure
VBCM.DeleteLines ProcStartLine, ProcLineCount
End If
Set VBCM = Nothing
End If
On Error GoTo 0
End Sub
Sub CallingProcedure()
'Declaring variables
Dim ModuleName, ProcedureName As String
'Getting value for module and procedure name from textboxes
ModuleName = Sheet1.TextBox1.Value
ProcedureName = Sheet1.TextBox2.Value
'Calling DeleteProcedureCode macro
DeleteProcedureCode ModuleName, ProcedureName
End Sub
- You have to save the code by selecting it and then close the window.

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

- Now, you have to make sure that your macro name is selected and click the *Run * button.

- Finally, after running the macro , you have to enter the following procedure from a module to delete.

Verdict
In the above post, you can learn the simple steps ** ** on how to delete a procedure from a module using VBA in Microsoft Excel. Kindly, share your feedback in the below comment section. Thanks for visiting Geek Excel. Keep Learning!
Read Ahead:
- How To Import a module from a file using VBA in Microsoft Excel?
- How To Delete module content using VBA in Microsoft Excel?
- Split Column through Excel office VBA – Easy tricks!!
- Excel Formulas to Find the First Row Number ~ Useful Tutorial!
- Find the Nth Largest Value in Excel Office 365 ~ Step-By-Step Procedure!!
Top comments (0)