PC Review


Reply
Thread Tools Rate Thread

Delete VB issue

 
 
LiAD
Guest
Posts: n/a
 
      25th May 2009
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

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      25th May 2009
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

"LiAD" wrote:

> 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
>

 
Reply With Quote
 
LiAD
Guest
Posts: n/a
 
      25th May 2009
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

"Joel" wrote:

> 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
>
> "LiAD" wrote:
>
> > 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
> >

 
Reply With Quote
 
LiAD
Guest
Posts: n/a
 
      25th May 2009
Got it sorted.

Thanks for taking a look

"LiAD" wrote:

> 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
>
> "Joel" wrote:
>
> > 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
> >
> > "LiAD" wrote:
> >
> > > 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
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
DELETE QRY issue William Microsoft Access Queries 4 5th May 2009 04:17 PM
Ctrl+alt+delete issue =?Utf-8?B?RnJlZA==?= Windows XP General 3 29th Jul 2005 02:38 AM
ISSUE: Using VBA to Delete a Column =?Utf-8?B?Y2luY29kZTU=?= Microsoft Excel Misc 0 26th Jan 2005 09:21 PM
Autonumber Issue Delete =?Utf-8?B?R2FyeQ==?= Microsoft Access Database Table Design 0 23rd Jan 2004 04:31 PM
Delete Query Issue James Monroe Microsoft Access 6 18th Nov 2003 04:51 AM


Features
 

Advertising
 

Newsgroups
 


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