Remove VBA code behind a worksheet

G

Guest

I have a worksheet with an event handler:

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo xitsub

If Target.Address = "$W$7" Then
After_AIF

Else

End If
xitsub:
End Sub

The After_AIF function is in "Module1" which I delete when the workbook is
saved:

'Delete VB Code
Dim x As Object

Set x = Application.VBE.ActiveVBProject.VBComponents
x.Remove VBComponent:=x.Item("Module1")

My question is how do I delete the "Private Sub Worksheet_Change(ByVal
Target As Range)" sub? I'm getting what is apparently a compile error before
the "new" workbook closes.

I've tried:

x.Remove VBComponent:=x.Item("worksheet").code and several variants of the
VBComponenet:= thingy without success..

TIA

ABC
 
J

Jim Rech

With ThisWorkbook.VBProject.VBComponents("Sheet1")
.CodeModule.DeleteLines 1, .CodeModule.CountOfLines
End With


--
Jim
|I have a worksheet with an event handler:
|
| Private Sub Worksheet_Change(ByVal Target As Range)
| On Error GoTo xitsub
|
| If Target.Address = "$W$7" Then
| After_AIF
|
| Else
|
| End If
| xitsub:
| End Sub
|
| The After_AIF function is in "Module1" which I delete when the workbook is
| saved:
|
| 'Delete VB Code
| Dim x As Object
|
| Set x = Application.VBE.ActiveVBProject.VBComponents
| x.Remove VBComponent:=x.Item("Module1")
|
| My question is how do I delete the "Private Sub Worksheet_Change(ByVal
| Target As Range)" sub? I'm getting what is apparently a compile error
before
| the "new" workbook closes.
|
| I've tried:
|
| x.Remove VBComponent:=x.Item("worksheet").code and several variants of the
| VBComponenet:= thingy without success..
|
| TIA
|
| ABC
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top