Delete VB issue

L

LiAD

Hi,

I have this code to try to delete whatever code is inside a file but it
doesn't seem to work. Would some-one be able to tell me why please or what
code I should use to delete whatever VB is in a file?

Thanks
LiAD


Sub DeleteAllVBA()

Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents

Set VBComps = ActiveWorkbook.VBProject.VBComponents

For Each VBComp In VBComps
Select Case VBComp.Type
Case vbext_ct_StdModule, vbext_ct_MSForm, _
vbext_ct_ClassModule
VBComps.Remove VBComp
Case Else
With VBComp.CodeModule
..DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp
End Sub
 
J

Joel

1) The activeworkbook cannot be the workbook where the macro is running. The
code cant delete itself.
2) Check you security seeting on the worksheet. Enable the option Trust
Access to the VBA Project Object model.

in 2003 it is on the menu Tools - Macro - Security Setting

in 2007 Excel OPtions - Trust Center - Trust Center stting
 
L

LiAD

Thanks.

The full code I have is attached below where there is a save as before the
delete VB part. Is there still a problem?

Security is (and was) enabled.

Thanks for your help

Sub copyclean()
'cleans formulas
For Each ws In Worksheets
ws.UsedRange.Value = ws.UsedRange.Value
Next ws
'savesAS
ActiveWorkbook.SaveAs Filename:="200905011_B", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False,
CreateBackup:=False
DeleteAllVBA
End Sub

Sub DeleteAllVBA()

Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents

Set VBComps = ActiveWorkbook.VBProject.VBComponents

For Each VBComp In VBComps
Select Case VBComp.Type
Case vbext_ct_StdModule, vbext_ct_MSForm, _
vbext_ct_ClassModule
VBComps.Remove VBComp
Case Else
With VBComp.CodeModule
..DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp
End Sub
 
L

LiAD

Got it sorted.

Thanks for taking a look

LiAD said:
Thanks.

The full code I have is attached below where there is a save as before the
delete VB part. Is there still a problem?

Security is (and was) enabled.

Thanks for your help

Sub copyclean()
'cleans formulas
For Each ws In Worksheets
ws.UsedRange.Value = ws.UsedRange.Value
Next ws
'savesAS
ActiveWorkbook.SaveAs Filename:="200905011_B", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False,
CreateBackup:=False
DeleteAllVBA
End Sub

Sub DeleteAllVBA()

Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents

Set VBComps = ActiveWorkbook.VBProject.VBComponents

For Each VBComp In VBComps
Select Case VBComp.Type
Case vbext_ct_StdModule, vbext_ct_MSForm, _
vbext_ct_ClassModule
VBComps.Remove VBComp
Case Else
With VBComp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp
End Sub
 

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