Indenting VBA code

Abgar

Active Member
Joined
Jun 20, 2009
Messages
265
Hi guys,

Silly question, but when i write VBA code (or rather - when i borrow portions of VBA code from other people to make my own), what are the rules around indenting to make it easier to read...

i.e. - what should be indented, and by how far etc etc

Code:
Sub a()
    'Indented Line
End Sub
 
Sub b()
'Non-Indented Line
End Sub

Just curious is all? :)

Cheers
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Abgar,

I think it is a personal preference.

I prefer two spaces.

A lot of people like 4 spaces.

You can change the setting in the VBA Editor:
click on Tools, Options
on the Editor tab, enter the value you want in the Tab width: box
then click on the OK button.
 
Upvote 0
Hi Abgar, heres my logic: (remember the purpose is to make it easier to interprit later on, and debug whilest developing the code)

I indent so as to form grouped bits of code using the idea that many statements have 2 parts. examples include...
For...Next
If...Else...End If
While...Wend
With...End With
Select Case...Case...End Select
Function...End Function
Sub...End Sub

i also include Open...Close which many will argue doesnt belong in this group, but i find it helpful

if you are coding and the 2nd part of a statement set does not line up exactly with its first part then you have probably made an error. indenting is especially useful in nested code, when you loose track of End If etc.
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,814
Members
449,339
Latest member
Cap N

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