ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Removing vba code from workbook (https://www.excelbanter.com/excel-programming/422934-removing-vba-code-workbook.html)

Jim[_74_]

Removing vba code from workbook
 
Is it possible to program saving the current workbook without the vba code ?

Regards & TIA




Mike H

Removing vba code from workbook
 
Hi,

Yes it is but it also very dangerous if you make an error. However, try
this. Put this in 'ThisWorkbook' which calls the code that does the deletion

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call DeleteCode
End Sub

Then this in a general module

Sub DeleteCode()
Prompt = "Are you certain that you want to delete all the VBA Code?"
Proceed = MsgBox(Prompt, vbYesNo + vbQuestion)
If Proceed = vbNo Then
Exit Sub
End If
On Error Resume Next
With ActiveWorkbook.VBProject
For x = .VBComponents.Count To 1 Step -1
.VBComponents.Remove .VBComponents(x)
Next x
For x = .VBComponents.Count To 1 Step -1
.VBComponents(x).CodeModule.DeleteLines 1, _
.VBComponents(x).CodeModule.CountOfLines
Next x
End With
On Error GoTo 0
End Sub


Mike



"Jim" wrote:

Is it possible to program saving the current workbook without the vba code ?

Regards & TIA






All times are GMT +1. The time now is 07:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com