使用 .DeleteLines 从模块 VBA 中删除特定代码

Use*_*716 4 excel vba

我想.DeleteLines在 VBA 中使用该功能。因为我没有删除模块中的所有行,所以我需要一种有针对性的方法。我假设有一个像 Find("FooBar").LineNumber 这样的函数,但是我在这里/谷歌找不到它:

https://msdn.microsoft.com/en-us/library/office/gg264546.aspx

Sub Deletings()
    With Workbooks("ClassExperiment.xlsm").VBProject.VBComponents("Module2").CodeModule
        .DeleteLines(HowDoIGetThisValue, 0)
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)

帮助表示赞赏。

Com*_*ern 5

如果您要移除整个过程中,你可以找到与该位置ProcStartLine财产,并与行数ProcCountLines

Dim module As CodeModule
Set module = Workbooks("ClassExperiment.xlsm").VBProject.VBComponents("Module2").CodeModule

Dim start As Long
Dim lines As Long
With module
    start = .ProcStartLine("button_Click", vbext_pk_Proc)
    lines = .ProcCountLines("button_Click", vbext_pk_Proc)
    .DeleteLines start, lines
End With
Run Code Online (Sandbox Code Playgroud)

警告:

这应该是显而易见的,但无论如何我都会把它扔掉。不要使用此(或任何其他方法)来更改在调试模式下运行代码的模块。这是打破工作簿的好方法。