Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-01-2019, 11:48 PM
afif afif is offline Organize Macro Codes for Reuse Windows 7 64bit Organize Macro Codes for Reuse Office 2010
Novice
Organize Macro Codes for Reuse
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default Organize Macro Codes for Reuse

I'm quite new to macro VBA. I want to reuse the codes I use in other documents.
How do you organize your code so they are reusable and easily identified ? Do you create modules, grouping procedures and function with similar functionality to a module ? Is there any naming convention for the procedure's, module's name ?


What are your best practices ?
Thanks
Reply With Quote
  #2  
Old 08-02-2019, 05:16 AM
kilroy kilroy is offline Organize Macro Codes for Reuse Windows 10 Organize Macro Codes for Reuse Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

For me it's all in the name I give the individual macro a very descriptive name or a commented out line of text so searching is easy. I save all in modules and back up all in word docs.
Reply With Quote
  #3  
Old 08-03-2019, 11:16 PM
Lugh's Avatar
Lugh Lugh is offline Organize Macro Codes for Reuse Windows 10 Organize Macro Codes for Reuse Office 2016
Competent Performer
 
Join Date: May 2019
Location: USA
Posts: 137
Lugh is on a distinguished road
Default

The simplest way is to have all your macros in the Normal template, so they're all automatically loaded into every doc. However, that can get cumbersome with a lot of macros.

Like Kilroy, I give each sub a very descriptive name, which can be long—clarity is the goal, eg:
Sub ReplaceGiraffeWithGorilla()
Quote:
How do you organize your code so they are reusable and easily identified ?
The main way I keep macros accessible & easily identified is by customizing the ribbon. I add a new tab or two, associate the key macros with buttons, and group them in ribbon sections.

When I have a lot of small routines which are part of a larger task, then the 'key macro' I mentioned will be one which calls each of the small ones—I give the key macros names which will sort at the top of the module display, in the order of execution.

Example:
Sub 1ReplaceAnimals ()
Call ReplaceGiraffeWithGorilla
Call ReplaceRhinoWithHippo
Call etc

Sub 2ReplaceBirds ()
Call ReplaceVultureWithCondor
Call ReplaceGooseWithDuck
Call etc

I have only a few macros in my Normal template, ie those I want to run every time I launch Word—eg AutoOpen & AutoNew to have the panels I want on screen from the start.

In case you don't know, macros are stored in Word templates. If you want the macros in your file system, then you have to copy & paste from the Word VBA editor. Make sure to locate the templates in a place you backup—you lose 'em, your macros are gone.

I keep most of my Office macros in separate templates, each of which is tailored to one of my main tasks. Each template contains only the macros relevant to that task. But that's just my preference.
Reply With Quote
  #4  
Old 08-04-2019, 01:09 AM
gmayor's Avatar
gmayor gmayor is offline Organize Macro Codes for Reuse Windows 10 Organize Macro Codes for Reuse Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Over the years I have created many add-ins, lots of which are available from my web site. I also use them to store macros that I use infrequently. To keep them readily available I created an add-in to handle them Load Word add-ins for the current session only which you may find useful.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 08-04-2019, 08:09 PM
afif afif is offline Organize Macro Codes for Reuse Windows 7 64bit Organize Macro Codes for Reuse Office 2010
Novice
Organize Macro Codes for Reuse
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default

That's a good one. Thanks Kilroy.
Reply With Quote
  #6  
Old 08-04-2019, 08:25 PM
afif afif is offline Organize Macro Codes for Reuse Windows 7 64bit Organize Macro Codes for Reuse Office 2010
Novice
Organize Macro Codes for Reuse
 
Join Date: Jul 2019
Posts: 16
afif is on a distinguished road
Default

Thanks Lugh, gmayor. That really helps.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
PPT 2016 refuses to copy/reuse slides from one pres. to another! Pantucci PowerPoint 0 08-18-2017 06:14 AM
Reuse slides deleting slide number and footer PSSMargaret PowerPoint 0 05-12-2016 05:28 AM
Auto-update field codes in Outlook Template - AutoOpen Macro not working victoriasun Outlook 5 12-22-2015 01:54 AM
Organize Macro Codes for Reuse Macro to read text file and organize data in excel srinidhi.mv88 Excel Programming 5 05-19-2015 12:06 AM
How do I keep graph sizing and location for reuse on a monthly basis lynchbro PowerPoint 0 06-09-2014 08:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft