|在本文中,我们将创建一个宏以从模块中删除另一个宏。

我们正在使用Module1,其中包含SampleProcedure作为要删除的示例宏。

ArrowMain

ArrowRawData

代码说明

设置VBCM = WB.VBProject.VBComponents(DeleteFromModuleName).CodeModule上面的代码用于创建已定义模块的对象。

ProcStartLine = VBCM.ProcStartLine(ProcedureName,vbext_pk_Proc)

上面的代码用于获取已定义过程的起始行号。

ProcLineCount = VBCM.ProcCountLines(ProcedureName,vbext_pk_Proc)

上面的代码用于获取已定义过程中的行数计数。

VBCM.DeleteLines ProcStartLine,ProcLineCount上面的代码用于删除定义过程中的所有行。

请遵循以下代码

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

如果您喜欢此博客,请在Facebook和Facebook上与您的朋友分享。

我们很希望收到您的来信,请让我们知道我们如何才能改善我们的工作并使您的工作更好。写信给我们[email protected]