DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How To Delete a procedure from a module using VBA in Excel?

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.

Sample data

  • 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
Select Visual Basic

  • 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

Enter fullscreen mode Exit fullscreen mode
  • You have to save the code by selecting it and then close the window.

Save the Code

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

Choose Macro option
Choose Macro option

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

Run the Code

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

Output

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:

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay