Quick and Dirty For Loops in the Immediate Window
Sometimes I want to loop through a set of items while I'm developing, troubleshooting, or debugging some code and it's not worth creating an actual Function or Sub routine. In those situations, I like to use the end-of-statement colon character (:
) to execute multiple statements–including entire For loops–in a single line of the Immediate Window.
Here's an example:
For i = 0 To CurrentDB.TableDefs.Count - 1: ?i, CurrentDB.TableDefs(i).Name: Next i
The single line of code above is equivalent to the following traditional code:
For i = 0 To CurrentDB.TableDefs.Count - 1
Debug.Print i & vbTab & CurrentDB.TableDefs(i).Name
Next i
I feel obliged to mention that while the code sample from the top of this article is perfectly valid syntax that you could use in a traditional code module, it's horribly unreadable* and should only be used for "quick and dirty" development use in the Immediate Window where it's guaranteed to be gone forever as soon as you exit the application (or overload the Immediate Window buffer).
* Kind of like this paragraph
Image by It is not permitted to sell my photos with StockAgencies from Pixabay