Jump to content
Sign in to follow this  
Ziggy54354

Code To Change Code

Recommended Posts

Is there a way to have a macro do a find and replace of code that is in a project? I'm changing the names of a bunch of bookmarks and I would like to automatically change the names of the bookmarks in the code to what I am changing them to. Does anyone know if this is possible?

Share this post


Link to post
Share on other sites

Find and replace / refactoring tools...

 

example as how it would be done in my favorite editor vim:

:%s/old_text/new_text/gc

 

That will replace all old_text (can be a regex) to new_text, and every occurrence it finds will prompt you if you want the change to be made... pressing y will change it, pressing n will move on.

Share this post


Link to post
Share on other sites

It totally depends on what you are using. I can't think of many editors that don't have a Find/Replace prompt or command though...

Share this post


Link to post
Share on other sites

Since you're working with VBA, then the easiest way would be to just use the built-in editor to do a find and replace. You can do a replace all if you're confident you know you won't accidental change something that you shouldn't. If you hit Ctl+H while in your project, you can specify to original text and replacement text, tell it where to search - current procedure, current project, etc., specify Match Case and Whole Word and then hit replace all.

 

If you have some abnormally large number of changes to make, you could write code to parse the code, but a manual search and replace would probably be done before you could write and test the code.

 

But, just for grins and giggles, you can export the code containing your macros to a text file - there's an export function under the File option on the menu bar. It'll have a .bas file extension, but is really a plain text file you can look at using any text editor like Notepad. You could then write VBA code to open the .bas file as a File System Object and parse the text to do a find and replace. Once you're done, you can import the file back into your project. Not too hard to do, but a lot more trouble than it's worth if you ask me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×