Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Deleting code at run time

I have two procedures, "Workbook_Open", and "Workbook_BeforeClose" in the
"ThisWorkbook" module. Is it possible to delete these procedures prior to
closing the workbook. In other words in my "save as" version I don't want
the code to run when the file is opened or closed.
Sandy


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Deleting code at run time

Sandy,

Try the code below, which requires a reference to MS VBA extensibility.

Note that the net result is that it simply comments out the code: I have never successfully deleted
code. Trying to delete code just isn't stable, in my experience.

HTH,
Bernie
MS Excel MVP

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim myBook As Workbook
Dim myVBA As VBIDE.VBComponent
Set myBook = ActiveWorkbook
Set myVBA = myBook.VBProject.VBComponents(myBook.CodeName)

With myVBA.CodeModule
For j = 1 To .CountOfLines
temp = "'" & .Lines(j, 1)
.DeleteLines j
.InsertLines j, temp
Next j
End With

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

End Sub



"Sandy" wrote in message
...
I have two procedures, "Workbook_Open", and "Workbook_BeforeClose" in the "ThisWorkbook" module. Is
it possible to delete these procedures prior to closing the workbook. In other words in my "save
as" version I don't want the code to run when the file is opened or closed.
Sandy



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Deleting code at run time

Absolutely brilliant. Where do you guys learn all this stuff?
Sandy

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Sandy,

Try the code below, which requires a reference to MS VBA extensibility.

Note that the net result is that it simply comments out the code: I have
never successfully deleted code. Trying to delete code just isn't stable,
in my experience.

HTH,
Bernie
MS Excel MVP

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim myBook As Workbook
Dim myVBA As VBIDE.VBComponent
Set myBook = ActiveWorkbook
Set myVBA = myBook.VBProject.VBComponents(myBook.CodeName)

With myVBA.CodeModule
For j = 1 To .CountOfLines
temp = "'" & .Lines(j, 1)
.DeleteLines j
.InsertLines j, temp
Next j
End With

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True

End Sub



"Sandy" wrote in message
...
I have two procedures, "Workbook_Open", and "Workbook_BeforeClose" in the
"ThisWorkbook" module. Is it possible to delete these procedures prior to
closing the workbook. In other words in my "save as" version I don't want
the code to run when the file is opened or closed.
Sandy





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting row using vb code? hol Excel Discussion (Misc queries) 4 November 30th 07 02:43 PM
VBA Code for Deleting a Row [email protected] Excel Worksheet Functions 3 September 6th 07 03:23 PM
Deleting Code from VBA Noemi Excel Discussion (Misc queries) 1 January 24th 06 08:55 AM
Another code for deleting row gregork Excel Programming 2 July 4th 04 06:29 PM
Deleting code in a file with code.. KimberlyC Excel Programming 3 March 4th 04 09:24 PM


All times are GMT +1. The time now is 02:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"