I have some vba procedures in a main.xls file. The main.xls adds new sheets to it and then takes these sheets and copies them to another workbook. The newworkbook.xls has the macro/vba code in it also. How do i delete it from these files OR how do i disable the macro message that comes up when the user opens the newworkbook.xls? thanks for the help
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Delete macros after they run (excel xp, win2000)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Delete macros after they run (excel xp, win2000)
- This topic has 12 replies, 7 voices, and was last updated 22 years, 10 months ago.
Viewing 2 reply threadsAuthorReplies-
H. Legare Coleman
AskWoody Plus -
WSjha900
AskWoody LoungerJuly 18, 2002 at 6:01 pm #601997Workbooks.Open Filename:=oldname & “data.xls”
‘code that adds 10 worksheets
‘code that does calculations and makes graphs on these worksheetsActiveWorkbook.SaveAs Filename:=NewFile & “data.xls”
ActiveWorkbook.Close SaveChanges:=TrueThe problems is that the NewFiledata.xls has the macros in it from oldfiledata.xls. When the user opens the file they get a message if they want to enable the macros. I needs to make this message stop appearing. For now I have been deleting the macros after i make newfiledata.xls but would like to automate this part. thanks
-
WSGary Frieder
AskWoody Lounger -
WSAndrew Cronnolly
AskWoody LoungerJuly 19, 2002 at 1:13 pm #602211Just copy all th eworksheets to a new workbook and save that with thye new file name. Unless you have code in the workbook object or in any of the sheet objects, then the code should not be part of th enew workbook. Include the following lines in red, in th eprocedure you have already posted.
Workbooks.Open Filename:=oldname & “data.xls”
‘code that adds 10 worksheets
‘code that does calculations and makes graphs on these worksheetsSheets.Copy
ActiveWorkbook.SaveAs Filename:=NewFile & “data.xls”
ActiveWorkbook.Close SaveChanges:=TrueThisWorkbook.Close SaveChanges:=False
Andrew C
-
-
-
WSKevin
AskWoody LoungerJuly 18, 2002 at 6:03 pm #601998You can write a mod that deletes all the code including itself. Add the VBA Extensibility Library to the references list and explore the the VBIDE objects.
You’ll have to experient a bit, but here’s the general idea. Don’t forget “on error” or call the code from a global or something. You might get an error after deleting the executing code.Dim vbComp As VBIDE.VBComponent
Dim vbComps As VBIDE.VBComponents
Sub test()
Set vbComps = Application.VBE.ActiveVBProject.VBComponents
Dim c As Integer
For Each vbComp In vbComps
c = vbComp.CodeModule.CountOfLines
While c > 0
vbComp.CodeModule.DeleteLines 1, c
Wend
Next
End Sub -
WSAndrew Cronnolly
AskWoody LoungerJuly 19, 2002 at 2:04 pm #602245Kevin,
A technicality in Excel (I do not know about Word) causes it to report the presence of Macros once a general or class module is present, even if empty. So removing the codelines would not inhibit the alert on opening the workbook. The following code should remove all modules, and any code in document codepanes such as worksheets and chartsheets. It also works without setting an explicit refernce to the Extensibility library.
Dim VBC, VBComp Set VBC = Application.VBE.ActiveVBProject.VBComponents For Each VBComp In VBC Select Case VBComp.Type Case Is 100 VBC.Remove VBComp Case Else With VBComp.CodeModule .DeleteLines 1, .CountOfLines End With End Select Next VBComp
Andrew
-
WSKevin
AskWoody Lounger -
WSTroyWells
AskWoody LoungerAugust 13, 2002 at 1:42 am #608001Andrew or anyone,
I found this codeworks great for me, but would like some understanding.
When I test this the first VBComp fits the Case Else statement. After this is complete, I don’t see any code modules disappear, so what is it deleting?
Why does Case Is 100 find entire modules? What is 100?
By the way, I’m using this with Word 2000. My slightly altered version of the codes is:
Dim VBC, VBComp
Set VBC = ActiveDocument.VBProject.VBComponents
For Each VBComp In VBC
Select Case VBComp.Type
Case Is 100
VBC.Remove VBComp
Case Else
With VBComp.codemodule
.deletelines 1, .countoflines
End With
End Select
Next VBCompThanks for the help!!
Troy -
WSAndrew Cronnolly
AskWoody LoungerAugust 13, 2002 at 7:42 am #608077Troy,
VB Components are made up of different types such as general modules, class modules and userforms together with the actual documentt object itseld, in Word ThisDocument. As the document objects can contain code (event procedures such as New, Open and Close and possibly others). As we cannot delete these document objects using this type of code (thankfully), we need to remove the code lines within those objects, and that is what happens when Case Else is executed.
As it happens the various VBComponents have a type property, and conveniently the document type is 100 so we can test for that find other types such as general modules (Type 1) calss modules (type 2) and userforms (type 3). So where the type is not 100 we can remove the component, code and all otherwise just remove the codelines.
There are constants to represent the component types (e.g a Document module is vbext_ct_Document, which evaluates to 100) but to use them a reference to the VBA Extensibility library is required. But I took shortcuts and perhaps some will not approve.
Try the following in both Word and Excel :
Dim VBC, VBComp
Set VBC = Application.VBE.ActiveVBProject.VBComponents
For Each VBComp In VBC
Debug.Print VBComp.Name & vbTab & VBComp.Type
Next VBCompWhen using it include userforms, general modules etc in the project.
Hope this helps,
Andrew
-
WSTroyWells
AskWoody Lounger
-
-
-
-
-
WSSammyB
AskWoody LoungerJuly 19, 2002 at 7:17 pm #602357Here’s some code that I used to remove all of the VBA code from all of the Excel and Powerpoint files that were open. You should get the idea from this. You want this code to reside in Personal.XLS so that it won’t delete itself. I think that you’ll need to add a reference to Microsoft Visual Basic for Applications Extensibility. HTH –Sam
Sub DeleteAllMacroCode() ' First Excel Dim xlBook As Excel.Workbook Dim rsp As Integer Dim vbComp As VBIDE.VBComponent For Each xlBook In Excel.Workbooks If UCase(xlBook.Name) "PERSONAL.XLS" Then ' Skip personal macro workbook rsp = MsgBox("Delete code in " & xlBook.Name, vbYesNoCancel, "Delete") If rsp = vbCancel Then Exit Sub If rsp = vbYes Then For Each vbComp In xlBook.VBProject.VBComponents If vbComp.Type = vbext_ct_Document Then With vbComp.CodeModule .DeleteLines 1, .CountOfLines End With Else xlBook.VBProject.VBComponents.Remove vbComp End If Next vbComp End If End If Next xlBook ' Now PoPo Dim ppApp As PowerPoint.Application Dim ppPres As PowerPoint.Presentation On Error Resume Next Set ppApp = GetObject(, "PowerPoint.Application") If Err.Number = 0 Then On Error GoTo 0 For Each ppPres In ppApp.Presentations rsp = MsgBox("Delete code in " & ppPres.Name, vbYesNoCancel, "Delete") If rsp = vbCancel Then Exit Sub If rsp = vbYes Then For Each vbComp In ppPres.VBProject.VBComponents If vbComp.Type = vbext_ct_Document Then With vbComp.CodeModule .DeleteLines 1, .CountOfLines End With Else ppPres.VBProject.VBComponents.Remove vbComp End If Next vbComp End If Next ppPres End If On Error GoTo 0 End Sub
-
WSjha900
AskWoody Lounger
-
Viewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Have Copilot there but not taking over the screen in Word
by
CWBillow
8 hours, 11 minutes ago -
Windows 11 blocks Chrome 137.0.7151.68, 137.0.7151.69
by
Alex5723
1 day, 12 hours ago -
Are Macs immune?
by
Susan Bradley
8 hours, 23 minutes ago -
HP Envy and the Function keys
by
CWBillow
20 hours, 24 minutes ago -
Microsoft : Removal of unwanted drivers from Windows Update
by
Alex5723
1 day, 23 hours ago -
MacOS 26 beta 1 dropped support for Firewire 400/800
by
Alex5723
2 days ago -
Unable to update to version 22h2
by
04om
14 hours, 45 minutes ago -
Windows 11 Insider Preview Build 26100.4482 (24H2) released to Release Preview
by
joep517
2 days, 7 hours ago -
Windows 11 Insider Preview build 27881 released to Canary
by
joep517
2 days, 7 hours ago -
Very Quarrelsome Taskbar!
by
CWBillow
1 day, 17 hours ago -
Move OneNote Notebook OFF OneDrive and make it local
by
CWBillow
2 days, 20 hours ago -
Microsoft 365 to block file access via legacy auth protocols by default
by
Alex5723
2 days, 9 hours ago -
Is your battery draining?
by
Susan Bradley
20 hours, 43 minutes ago -
The 16-billion-record data breach that no one’s ever heard of
by
Alex5723
7 hours, 42 minutes ago -
Weasel Words Rule Too Many Data Breach Notifications
by
Nibbled To Death By Ducks
3 days ago -
Windows Command Prompt and Powershell will not open as Administrator
by
Gordski
10 hours, 38 minutes ago -
Intel Management Engine (Intel ME) Security Issue
by
PL1
2 days, 8 hours ago -
Old Geek Forced to Update. Buy a Win 11 PC? Yikes! How do I cope?
by
RonE22
2 days, 1 hour ago -
National scam day
by
Susan Bradley
1 day, 7 hours ago -
macOS Tahoe 26 the end of the road for Intel Macs, OCLP, Hackintosh
by
Alex5723
2 days, 4 hours ago -
Cyberattack on some Washington Post journalists’ email accounts
by
Bob99
4 days, 1 hour ago -
Tools to support internet discussions
by
Kathy Stevens
2 days, 14 hours ago -
How get Group Policy to allow specific Driver to download?
by
Tex265
3 days, 16 hours ago -
AI is good sometimes
by
Susan Bradley
4 days, 8 hours ago -
Mozilla quietly tests Perplexity AI as a New Firefox Search Option
by
Alex5723
3 days, 22 hours ago -
Perplexity Pro free for 12 mos for Samsung Galaxy phones
by
Patricia Grace
5 days, 9 hours ago -
June KB5060842 update broke DHCP server service
by
Alex5723
5 days, 7 hours ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
5 days, 11 hours ago -
Excessive security alerts
by
WSSebastian42
4 days, 2 hours ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
5 days, 21 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.