IndentCode


Description:
This function re-indents code passed to it, based on where blocks of code should be. I find this very useful when I'm trying to follow someone else's code that doesn't use tabs (or uses them oddly). Note: This code uses the clsStringCollection class, it's available under the Strings->Split on my sample code page.
 
Code:
Option Explicit

Private Const TAB_SIZE = 4

Public Function IndentCode(sCode As String) As String

    Dim sc As clsStringCollection
    Set sc = New clsStringCollection
    sc.SplitInto sCode, vbNewLine
    
    Dim nCurLine As Long
    Dim nCurTab As Long
    Dim sLine As String
    Dim vItem As Variant
    Dim bEscapeEOL As Boolean
    Dim nCount As Long
    Dim nEOLTab As Long
    Dim vLine As Variant
    Dim sOut As String
    
    Dim colIncrease As Collection
    Dim colDecrease As Collection
    Dim colInAll As Collection
    Dim colDeAll As Collection
    Set colIncrease = New Collection
    Set colDecrease = New Collection
    Set colInAll = New Collection
    Set colDeAll = New Collection
    
    colIncrease.Add "If "
    colIncrease.Add "With "
    colIncrease.Add "Select Case "
    colIncrease.Add "Select Case "
    colIncrease.Add "While"
    colIncrease.Add "Do"
    colIncrease.Add "Private Sub "
    colIncrease.Add "Private Function "
    colIncrease.Add "Private Property "
    colIncrease.Add "Public Sub "
    colIncrease.Add "Public Function "
    colIncrease.Add "Public Property "
    colIncrease.Add "Sub "
    colIncrease.Add "Function "
    colIncrease.Add "Property "
    colIncrease.Add "For "
    colIncrease.Add "Type "
    colIncrease.Add "Public Type "
    colIncrease.Add "Private Type "
    
    colDecrease.Add "End If"
    colDecrease.Add "End Select"
    colDecrease.Add "End Select"
    colDecrease.Add "End With"
    colDecrease.Add "Wend"
    colDecrease.Add "Loop"
    colDecrease.Add "End Sub"
    colDecrease.Add "End Function"
    colDecrease.Add "End Property"
    colDecrease.Add "Next"
    colDecrease.Add "End Type"
    
    colDecrease.Add "Else"
    colDecrease.Add "Case"
    colIncrease.Add "Else"
    colIncrease.Add "Case"
    
    nCurTab = 0
    bEscapeEOL = False
    nCount = sc.Count
    
    For Each vLine In sc
        sLine = Trim(vLine)
        
        For Each vItem In colDecrease
            If LCase(Mid(sLine, 1, Len(vItem))) = LCase(vItem) Then
                nCurTab = nCurTab - TAB_SIZE
            End If
        Next
        
        If nCurTab < 0 Then
            nCurTab = 0
            sLine = sLine & " ' TAB COUNT ERROR"
        End If
        
        
        sOut = sOut & Space(nCurTab) & sLine & vbNewLine
        
        If Right(sLine, 2) = " _" Then
            If Not bEscapeEOL Then
                nEOLTab = InStr(1, Trim(sLine), " ")
                If nEOLTab < 3 Then
                    nEOLTab = 3
                End If
                nCurTab = nCurTab + nEOLTab
                bEscapeEOL = True
            End If
        Else
            If bEscapeEOL Then
                nCurTab = nCurTab - nEOLTab
                bEscapeEOL = False
            End If
        End If
        
        For Each vItem In colIncrease
            If LCase(Mid(sLine, 1, Len(vItem))) = LCase(vItem) Then
                nCurTab = nCurTab + TAB_SIZE
            End If
        Next
        
    Next
    
    IndentCode = sOut
    
End Function
 
Sample Usage:
 
    Dim sTemp As String
    sTemp = IndentCode(Clipboard.GetText)
    
    Clipboard.Clear
    Clipboard.SetText sTemp