Search code examples
excelvbaline-separator

What reason(s) NOT to place multiple lines of VBA code on a single line separated by colons?


We are building a local library of VBA functions at our business, and the length of code in several modules has become a bit lengthy, and there is no (known) method collapse sections of code. We have started to consolidate the simpler functions (tested, known-good, simple functions) into colon-delimited line(s), for the ability to easily scroll through code. Question is, what are the reasons NOT to do so? The only thing we've found is that breakpoints/debugging may at worst be a hurdle, but the code is solid, and we could split the lines out should there ever be a need for a deep dive. Anybody with experience, ideas, suggestions on this? TIA

Obv: text

and text


Solution

  • Question is, what are the reasons NOT to do so?

    If statements.

    As pointed out in your 2nd example - but not noted in your question. There are workarounds, but almost any code will have a few If statements in it.

    Consider the following code:

    For i = 0 To n
        If a = i Then a = b
    Next i
    

    The If statement is effectively:

    If a = i Then [a = b]
    

    This is because anything after the Then (until an optional Else or End If) is performed when a = i.

    So if you attempt to consolidate this with colons, like so:

    For i = 0 To n : If a = i Then a = b : Next i
    

    .. then it compiles to:

    For i = 0 To n : If a = i Then [a = b : Next i]
    

    So the Next i is only performed where a = i.

    As the link you provided does note, there are alternatives. But most people like using If.