VBA Formatting/Indenting

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
Hi All,

Wondering if someone could point me to some information on proper formatting/indentation of VBA code in Excel. I'm proficient in VBA but never learned proper formatting and my code always looks like junk compared to others.

Thanks in advance,

Ryan
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Wondering if someone could point me to some information on proper formatting/indentation of VBA code in Excel. I'm proficient in VBA but never learned proper formatting and my code always looks like junk compared to others.
The general rule I follow is this... if the statement has a closing "companion" (such as "Sub/End Sub", "If/End If", "For/Next", "Open/Close", "Do/Loop", etc.), then I indent everything in between the the same amount. Also, to make things easier, I write the closing "companion" right after I write its "opening" companion. So, using For..Next as an example, I write all three lines (the empty line between them... produced by pressing Enter twice)...
Code:
For X = 1 To Whatever

Next
I then then use the up arrow to put the text cursor on the empty line, press my two spaces (what I use to indent my code; I have seen other prefer 4 spaces) and then start typing the code between them. Pressing Enter at the end of each statement helps maintain the offset levels for you. When done with the loop code, I cursor down twice to end up on the line after the Next statement (and again, VB helps keep the indent level correct for you). If you prefer, you can use the Tab key for your indents instead of hitting the space bar multiple times by clicking Tools/Options on the VB editor's menu bar and setting the number of spaces the Tab key should indent in the "Tab Width" field on the Editor tab.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top