Excel vba delete sub code
<p>Regarding VBE.
If a worksheet does not exist it will create it and add a commandbutton at Sheet1. It will also add the appr. code for the private sub commandbutton1_click.
This works all fine. This is part of the existing code that will add a new sub:</p>

<pre><code>Dim Code as String
Code = "Private sub CommandButton1_Click()" &amp; vbCrLf
[..]
Code = Code &amp; "End Sub"

With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
    .insertlines .CountOfLines + 1, Code
</code></pre>

<p>However before adding that 'sub code', it should check if in the already existing (object) the sub code (for commandbutton1) already exists and if so, then delete it before adding new code.</p>