Good Tip!
Main Topics
Browse All TopicsHow do I change my macro code with a macro in a separate workbook?
My situation is this: I made a macro that seemed to work just fine. The user(s) of my macro ran it for a while without experiencing any problems. There are now 30-40 copies of this macro workbook in one directory, each with different information that the macro pulled in (they are for different companies). Suddenly, they realized that something wasn't quite how it was supposed to be, so I had to change about two or three lines of code in my macro. I now have an updated workbook, but I don't want to have to copy and paste the code into all the old workbooks. And I certainly don't want to re-run the new macro to import the data for all 30-40 companies that we already did.
Is there some way that I can simply open the workbook through code (that's easy, I know how to do that), access the VBA code in that workbook, then change that code (or change just a part of it, like one or two lines in one or two subroutines) with my macro?
This is rather urgent, if I can get a response to this question this morning, that would be great. If I don't get a response by around 1-2pm, I'll have to do it all manually (feel free to respond after that, but I'll be busy working, so I probably won't respond or anything for a little while).
Regards,
PtG
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi PaultheGreat,
Yes you can
Have a look at these examples
http://www.j-walk.com/ss/e
http://www.cpearson.com/ex
Although be warned, I once created some code to copy code between workbooks and McAfee thought it may have been a virus and it deleted it :)
Cheers
Dave
Here is another link if you get bored with Mr Walkenbach & Mr Pearson
http://www.erlandsendata.n
Paul,
Here's an example:
This code will change the VBA code in Module1 of the workbook c:\test\changeme.xls
I believe that you need to set a reference to the library
"Microsoft Visual Basic for Applications Extensibility 5.x"
Sub ChangeVbaCode()
Dim wb As Workbook
Dim proj As VBIDE.VBProject
Dim module As VBIDE.CodeModule
Set wb = Workbooks.Open("c:\test\ch
Set proj = wb.VBProject
Set module = proj.VBComponents("Module1
module.ReplaceLine 4, " MsgBox ""Changed message"""
module.InsertLines 5, " MsgBox ""A new message"""
wb.Close SaveChanges:=True
End Sub
Make sure that Module1 of ChangeMe.xls contains this code:
Option Explicit
Sub MyProcedure()
MsgBox "Hi there"
MsgBox "Hi again!"
End Sub
Ture Magnusson
Karlstad, Sweden
hey, a little help with this here:
Do you guys know what "Dr. Watson for Windows NT" is? cuz he's driving me nuts! he keeps giving me an access violation error when I try to insert lines into a module... and then an error log is generated and Excel is forced to exit! Please help, the fate of the universe depends on me being able to insert lines into a module! :-)
lol i'm an intern! i'm only 18 yrs old! unfortunately, a large worldwide company like the one for which I'm working decided not to set me up with an admin account :-)
here's the code:
Dim temp As VBComponent, temp1 As VBComponent
FName = ThisWorkbook.Path & "\ThisWorkbook.cls"
curWB.VBProject.VBComponen
Set temp = curWB.VBProject.VBComponen
Set temp1 = curWB.VBProject.VBComponen
temp.CodeModule.DeleteLine
temp.CodeModule.InsertLine
curWB.VBProject.VBComponen
FName = ThisWorkbook.Path & "\Module1.bas"
curWB.VBProject.VBComponen
Set temp = curWB.VBProject.VBComponen
Set temp1 = curWB.VBProject.VBComponen
temp.CodeModule.DeleteLine
temp.CodeModule.InsertLine
curWB.VBProject.VBComponen
curWB is set earlier to a workbook... both files ThisWorkbook.cls and Module1.bas exist, are good.
Dr. Watson pops up on the first line that says "temp.CodeModule.InserLine
i appreciate the "maybe i can find it" comment, but i don't really like the continuation: "later at home"...
an interesting thing happened... I did as you suggested, bruintje, and put the code in a clean workbook... it worked! but wait... when i try to include any of my other code, it doesn't work! gah! and i need to include the other code for it to be of any practical use. yes, i realize that by now i could've finished the whole thing manually... but i'm a programmer! i shall not succumb to manual labor! and yes, i realize that since all i'm doing is using a keyboard and mouse with my hands, i am doing manual labor... but back to the problem:
i need to allow the user to select a bunch of workbooks, and then my macro will open each of those workbooks and change the macro in them. this shouldn't be a problem, right? here's my full code [...... I was about to put my code here... then it miraculously worked!]
explanation: I deleted a few lines of COMMENTS! and that FIXED it! What is WRONG with Excel?!? thank you very much for your help! i will split up the points in a little bit when i get time, probably after lunch.
Thank you everyone!
Warmest regards to all who helped,
PtG
alright, for some reason this code is no longer working... I had it working for a little bit, but when I tried to add more stuff into it, it just quit. I have been trying many more times, copying code into new workbooks, using code that used to work just fine... but all to no avail. i cannot get it to function correctly, i keep getting evil Dr. Watson... so for now I have to do this manually... I would still like to get the macro working so i can use it in the future... btw, i've tried plenty of things like restarting, making sure nothing else is running at the same time, trying it on a different comp... nothing seems to work.
thank you very much, though, bruintje and brettdj, for providing links to resources that helped me learn how i should be able to do what i want to do... if it would work! :-P no wonder people make things like the blaster worm: they get sick of M$ apps failing for no reason and decide to harness the power of millions of computers to attack the M$ website... lol too bad they thought of it before me :-) (not that i could/would do that if it hadn't been done before...)
Regards,
PtG
Business Accounts
Answer for Membership
by: bruintjePosted on 2003-08-28 at 06:56:50ID: 9240408
try
cel/vbe.ht m
title: programming to the vbe
source : http://www.cpearson.com/ex
hope this helps a bit