Repost: Custom Color VBA


Posted by Bagsy Baker on February 06, 2002 2:01 PM

Here's one that I have not been able to find a post for, and any ideas would help.
I have recently started to write longer, more involved VBA code. What I would like to do is colour code certain parts (ie. For...Next Loops, Dim's, etc.) various colurs so that I can pick them out easier within the module. I try to give meaningful names to my dims, but don't want to duplicate a name. So far, all I've been able to do is CTRL+F, "Dim" and run through the list. Any ideas?

Faster posted a possible answer(use lots of *'s), but it adds much to the printed module and is cluttered.
Any other ideas?
Maybe an add-in?

Posted by JACK INT THE Uk on February 06, 2002 2:21 PM

There are sections of code that can be coloured, but i never bother sorry, check help files.. [NT]

Posted by Capulet Mercutio on February 06, 2002 4:04 PM


You can find the available colour options under Tools>Options>EditorFormat in the VBE



Posted by Mark O'Brien on February 06, 2002 5:14 PM

Indenting your code is the easiest way to make it look pretty and easy to look through. e.g. When I do a For...Next Loop the next line of code after "For" is indented 2 spaces. DO this for If..Else..End If as well. Every time you nest a loop or If..Else..End If, simply add an extra couple of spaces. (I'd put an example, but this msgbrd chops off leading spaces in sentences.)

As far the "dim" statements, it's easiest to put this at the start of a Subroutine or Function. e.g. I usually start all of my routines like this (including the comments):

' Declare Variables

Dim MyName as String
Dim i as integer

' Initialise Variables

MyName = "Mark"
i = 20

Finally, if you want an easy way to look at code for simply a subroutine or function in a module, you can switch to "Procedure View" in the module. You do this by clicking on the "Procedure View" button that is located in the bottom left corner of the code window.

HTH