Tidy VBA code indentation

Having your code follow generally accepted indentation rules makes it much more readable and easier to debug. When Code VBA inserts code its placement is determined by the current position of the cursor, it does not take the rest of the procedure in account. For this we have the Code VBA 'Tidy indentation' feature. With Code VBA 'Tidy indentation' feature you can have your code formatted automatically or only when you explicitly ask for it.

Automatic code indentation

Automatic tidying of a procedure's code indentation is done when you move out of the procedure you have been editing. This feature requires you to have the preference 'Tidy indentation when exiting a procedure' set: from the toolbar, select Code VBA » Preferences  » Tidy indentation (...)

The animation below shows the messy code automatically being aligned when moving to another procedure.

clean code indentation automatic

Tidy procedure action in the Code Explorer

If for some reason you don't want to always have code automatically tidied, you can also tidy individual procedures or a complete module from the Code Explorer menu - see screencast below.

clean code indentation manually

Typical indentation anomalies being fixed:

  • Dim objUser not outlines, same with some other variables
  • End If not outlines with original If Not ammProcesses..., making it difficult to see what scope the block has
  • For Each block unclear - difficult to see where it ends.