> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# All About Indenting
- URL: https://nolongerset.com/all-about-indenting/
- Published: 2023-01-11T03:45:36.000Z
- Updated: 2026-05-08T12:52:26.000Z
- Description: Do you obsess over the smallest details of the code-writing process? If not, you might want to skip this article. Don't say I didn't warn you...
- Author: Mike Wolfe
- Tags: Basic, VBA, #Import 2026-05-20 02:59

Today's article is a ~~quick~~ practical tip about the code-writing process.

Proper indenting is one of the most important parts of creating readable code. Each logical "block" of code should be indented to distinguish it from the code that comes before and after it. Code blocks are often nested, leading to multiple levels of indentation.

Here's a quick example:

```vba
'Contrived routine that lists all the code modules
'   in the current project and whether or not
'   they are open in the VBA IDE
Sub ListModules()
    With CurrentProject
        Dim AccObj As AccessObject
        For Each AccObj In .AllModules
            If AccObj.IsLoaded Then
                Debug.Print AccObj.Name; " is loaded"
            Else
                Debug.Print AccObj.Name; " is not loaded"
            End If
        Next AccObj
    End With
End Sub
```

The above code has four blocks. From the outside in, they are:

- `Sub` ... `End Sub`
- `With` ... `End With`
- `For Each` ... `Next`
- `If` ... `Else` ... `End If`

When I write code with these sorts of blocks, I use the following approach:

1. Write the opening line
2. Press \[Enter\] twice
3. Write the closing line
4. Press \[Up\] then \[Tab\]
5. Write the inner code

## Practical Example

Here's how the above code would evolve in my development environment. (*I use the pipe character \[*`|`*\] in the code samples below to represent the position of the cursor.*)

### Detailed Breakdown of the First Block

```vba
Sub ListModules()
    With CurrentProject|
End Sub
```

1\. Write the opening line

```vba
Sub ListModules()
    With CurrentProject
    
    |
End Sub
```

2\. Press \[Enter\] twice

```vba
Sub ListModules()
    With CurrentProject
        
    End With|
End Sub
```

3\. Write the closing line

```vba
Sub ListModules()
    With CurrentProject
        |
    End With
End Sub

```

4\. Press \[Up\] then \[Tab\]

```vba
Sub ListModules()
    With CurrentProject
        Dim AccObj As AccessObject|        
    End With
End Sub
```

5\. Write the inner code

### Block-by-Block Overview

```vba
Sub ListModules()
    |
End Sub
```

Start with the subroutine block

```vba
Sub ListModules()
    With CurrentProject
        |
    End With
End Sub
```

Add the `With` ... `End With` block

```vba
Sub ListModules()
    With CurrentProject
        Dim AccObj As AccessObject
        For Each AccObj In .AllModules
            |
        Next AccObj
    End With
End Sub
```

Add the `For Each` ... `Next` block

```vba
Sub ListModules()
    With CurrentProject
        Dim AccObj As AccessObject
        For Each AccObj In .AllModules
            If AccObj.IsLoaded Then
                |
            End If
        Next AccObj
    End With
End Sub
```

Add the `If` ... `End If` block

```vba
Sub ListModules()
    With CurrentProject
        Dim AccObj As AccessObject
        For Each AccObj In .AllModules
            If AccObj.IsLoaded Then
                Debug.Print AccObj.Name; " is loaded"
            Else
                Debug.Print AccObj.Name; " is not loaded"
            End If
        Next AccObj
    End With
End Sub
```

Finish populating the `If` ... `Else` ... `End If` block

## Why Do I Do It This Way?

This approach helps reduce compile errors, but that in itself is not a big deal. [Compile errors](https://nolongerset.com/compile-errors/) are [cheap and easy to fix](https://nolongerset.com/some-bugs-are-better-than-others/).

Where this approach helps the most is in avoiding off-by-one type [logic errors](https://nolongerset.com/logic-errors/), such as ensuring a variable is being incremented on the correct side of a loop. This *is important*, as logic errors are the most difficult and expensive bugs to fix.

*(That said, the best way to write code that's easy to read and to avoid block-related logic errors is to minimize the number of indented levels in your code in the first place. For example, if you have a procedure with six levels of indentation, you can probably split the three inner code blocks out into a standalone procedure and cut your levels of indentation in half.)*

## Reader Feedback

I assume that most developers use this approach, but I could be wrong. 

Newer development environments (i.e., not VBA) often have a code-block-autocomplete feature built-in or available via third-party plugins. Thus, this might be a problem somewhat unique to VBA in the year of our Lord 2023.

Do you use a similar approach when writing code in VBA? If not, what approach do you use? Or do you not really think about it? Maybe I'm just a weirdo for obsessing over this level of programming minutiae. Let me know in the comments below.

---

### Referenced articles

[Some Bugs are Better than OthersNot all bugs are created equal. Avoid the expensive ones by making more of the ones that are easy to find and fix.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/03/car-2122580_1920.jpg)](https://nolongerset.com/some-bugs-are-better-than-others/)