Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

There is a way to run multiple lines of code on one line in VBA and another way to separate one line of VBA code across multiple lines.

I find in particular when using the Immediate Window in the VBE editor, that if I want to test a class I’m using there, I need to be able to run multiple lines and be able to hit enter once.

For example, consider these two lines:

Set testVar = New MyClass
Debug.Print testVar.TestMethod

Trying to run each one individually in the Immediate window doesn’t work because the environment gets reset each time.  But if I put them all on one line using the “:” operator, they will both run in one Immediate window operation.

Set testVar = New MyClass : Debug.Print testVar.TestMethod

This will print whatever the TestMethod function returns.

If you want to split lines, you can use the “_” operator like this:

Debug.Print _
"This will " & _
"run when you " & _
"hit enter on this last line."

Each time you use the underscore character, it will wait until you stop using the underscore character at the end of the line.  So using my first example combined with the line continuation you could do something like this:

Set testVar = New MyClass : _
Debug.Print testVar.TestMethod

And it will run both lines after you hit enter on the second line.