Remove specific code from the VBA module using .DeleteLines

I would like to use a function .DeleteLinesin VBA. Since I do not delete all the lines in the module, I need a targeted approach. I assume that there is a function like Find ("FooBar"). LineNumber, however I cannot find it here / using google:

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

Help evaluate.

+4
source share
2 answers

If you delete the entire procedure, you can find its location using the ProcStartLine property and the number of lines with 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

Attention:

, . ( ) , . .

+4
Sub test()

Dim vb As VBComponent
Dim i As Integer

Set vb = ThisWorkbook.VBProject.VBComponents("Module2")

For i =vb.CodeModule.CountOfLines to 1 step -1

    If InStr(1, vb.CodeModule.Lines(i, 1), "'   remove") <> 0 Then
        vb.CodeModule.DeleteLines i, 1
    End If

Next i

End Sub

, , , ? , , , .

+2

Source: https://habr.com/ru/post/1650044/


All Articles