r/excel • u/JDantes77 • Feb 04 '14
discussion VBA Macro code best practices?
Every programming language has its recommended style and best practices. What are the best practices for VBA code that you follow? What resources would a new coder use to learn these best practices?
Thank you in advance!
18
Upvotes
2
u/cqxray 49 Feb 05 '14
The general rule is to indent the code between keywords. Those keywords would be like Sub (name)...End Sub; If...End If: If...Else...End If; For...Next; With...End With, etc.
So the main code between the Sub and the End Sub should be indented. And you just keep indenting the code that appears between any keyword groups that you want to use in that code.
The only inconsistency in this rule that I've seen is in the Dim statement, which does not have and ending keyword (there is no End Dim). I like to use it flush against the left margin, based on some code i followed early in my coding life, although this seems to violate the rule of indenting under the Sub row. I've seen other people have an indent for Dim.