Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific code from a module VBA using .DeleteLines

Tags:

excel

vba

I'd like to use the .DeleteLinesfunction in VBA. As I'm not deleting all the lines in the module i need a targeted approach. I assume there is a function like Find("FooBar").LineNumber, however I can't find it here/with 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 appreciated.

like image 731
Preston Avatar asked Mar 12 '23 13:03

Preston


1 Answers

If you're removing the entire procedure, you can find its location with the ProcStartLine property and the line count 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

Warning:

This should be obvious, but I'll throw it out there anyway. Do not use this (or any other method) to alter the module that the code is running from in Debug mode. This is a good way to break your workbook.

like image 74
Comintern Avatar answered Mar 23 '23 06:03

Comintern