VBA – Debug.Print – spc, tab, semi-colon, comma and more

I think one of the most underutilized methods for developers is the Print method! Beyond which, those that do use it often don’t necessarily use it to its full potential.

Did You Know?
Did you know the Print method actually has parts (options) that you can use? Most developer don’t.
 

What Is The Print Method?

The print method allow us to output (or ‘print’) information/values/content to VBA/VBE the Immediate Window.
 

How Is That Useful?

I know developers that don’t see the utility of Print when they have MsgBox, but they serve 2 distinct purposes.

MsgBox is more for the end-user, to display a message to them. Sadly, you can interact with a MsgBox. Amongst other things, you can’t copy the contents.

Print is only for the developer as it is displayed in the VBE Immediate Window.  The beauty here is:

  • that you can copy/paste its content which is great to search up those error numbers/descriptions.
  • use it while in the VBE creating code without having to switch back and forth to the Access GUI and VBE windows
  • using the options, and other techniques, you can format the output.
  • you can sprinkle it throughout procedure to see the evolution of variables, … in real-time. This is great for debugging purposes.

 

Basic Usage

Its most basic usage would be

a = "Some"
b = "Value"
Debug.Print a & " " & b

which will the return (in the VBE Immediate Window)

Some Value

 

Blank Line

There are times that it is useful to insert separation lines, a blank line and this is very easy to do!  Simply do either

Debug.Print

OR

Debug.Print ""

 

Options You Say

If you look at the documentation for a moment, you will see they mention Spc(n), Tab(n) & ;, so what are these and how do they work.

Spc

Used to insert space characters in the output, where n is the number of space characters to insert.Microsoft

So using Spc(n) you can insert n number of space in front of value, after the value or on it’s own.

Debug.Print Spc(12)

will return what appears to be a blank line, when in fact there are 12 space characters on it

            

Now realistically, no ones really going to output a series of spaces on their own like the example above, well not very often at least, but this can be used to pad/align content.

a = "Some"
b = "Value"
Debug.Print Spc(12); a
Debug.Print a; Spc(12)
Debug.Print a; Spc(12); b

which then outputs something along the lines of

            Some
Some            
Some            Value

where the

  • value ‘Some’ starts as the 13th character on the 1st line
  • 2nd line has 12 training spaces.
  • there is a 12 space gap between the words on the 3rd line

Tab(n)

Used to position the insertion point at an absolute column number where n is the column number.Microsoft

So, in plain English, somewhat like the spc(), tab enables you to position the output to a specific position (column).  Pay particular attention to the term absolute!  so it is not in function of a relative position, but the absolute position which is great for fixed position output.

Thus, we can quickly learn that

Debug.Print a

is the same as

Debug.Print Tab(1); a

as Tab(1) indicates to output the content on the 1st column.

You can also use Tab() to add whitespaces

a = "Some"
b = "Value"
Debug.Print a; Tab(10)

 

Some     

In the above, it adds 5 spaces, so the next available character would start on the 10th column. Thus, we could use it for fixed length output

a = "Some"
b = "Value"
Debug.Print a; Tab(10); b

 

Some     Value

Semi-Colon (;)

Let’s talk about the colon (;)!

Use a semicolon (;) to position the insertion point immediately following the last character displayed.Microsoft

Normally, when you perform a Print, it outputs the ‘value’ and then you start fresh on a new line.  The ; is a continuation operator telling the output to continue from the current position.  Thus, it allows us to continue to output content on the same line as the previous output.  Now, the example above doesn’t do it justice, so let’s try another more concrete example

a = "Some"
b = "Value"
   
Debug.Print a;
Debug.Print Spc(12);
Debug.Print b

which outputs

Some            Value

So we can now split the Debug.Print into multiple lines while having the output remain on a single line.

This can be very helpful when building long output.  Rather than creating a really long variable, string, … you can simply Debug.Print each on their own line, but use the ; so they continue to print out on the same line in the VBA/VBE Immediate Window.

Here’s a real example

        With oCol
            Debug.Print .Caption
            Debug.Print , .Description;
            Debug.Print , .Domain;
            Debug.Print , .InstallDate;
            Debug.Print , .LocalAccount;
            Debug.Print , .Name;
            Debug.Print , .SID;
            Debug.Print , .SIDType;
            Debug.Print , .Status
        End With

So the outputs 2 lines, one with just the Caption and a 2nd line with all the details for that entry. While Debugging, this format is much easier to read and tweak.

Here’s one more example, used to output the content of a recordset so that each record is on a single line.

Do While rs.EOF = False
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs(i) & ",";
    Next i
    Debug.Print
    rs.MoveNext
Loop

 

Comma (,)

The comma!  The comma performs an indentation.  Like the spc() & tab() it can be used to aid legibility, add indentations …

Its usage is very straightforward.  Let’s pretend we’re gathering Disk Drive information and breaking down the partitions by drive.  Now let’s output it using some of the techniques we’ve discussed thus far

a = "Disk 1"
b = "Partition 1"
c = "Partition 2"
d = "Partition 3"
e = "Disk 2"
f = "Partition 1"
g = "Partition 2"

Debug.Print a & " " & b & " " & c & " " & d & " " & e & " " & f & " " & g
Debug.Print
Debug.Print a
Debug.Print b
Debug.Print c
Debug.Print d
Debug.Print e
Debug.Print f
Debug.Print g
Debug.Print
Debug.Print a
Debug.Print , b
Debug.Print , c
Debug.Print , d
Debug.Print e
Debug.Print , f
Debug.Print , g

this will then output something resembling

Disk 1 Partition 1 Partition 2 Partition 3 Disk 2 Partition 1 Partition 2

Disk 1
Partition 1
Partition 2
Partition 3
Disk 2
Partition 1
Partition 2

Disk 1
              Partition 1
              Partition 2
              Partition 3
Disk 2
              Partition 1
              Partition 2

I think it is pretty evident how useful the comma (,) is to making the output MUCH more legible!
 

Printing In The Immediate Window

There are times, debugging being the most common, when you wish to perform a Debug.Print directly in the Immediate Windows itself.  Here we can simplify things and instead of doing

Debug.Print a

We can simply do

? a

The ? replaces Debug.Print when entered directly in the Immediate Window!
 

Limitations

Sadly, you cannot use a With statement, thus something like

With Debug
    .Print a
    .Print b
    .Print C
    .Print d
    .Print e
    .Print f
    .Print g
End With

does not work.

Nor can you just use .Print on its own.

Nor can you use the ? directly in VBA code as it only works in the Immediate Window directly.
 

Useful Resources

5 responses on “VBA – Debug.Print – spc, tab, semi-colon, comma and more

  1. Lukas Rohr

    Very nice summary! I use Debug.Print for almost everything I do and especially while I’m developing.
    I wasn’t aware of the comma. That will be useful in the future. There is also the option of using vbTab instead of the Tab() function.

    1. Daniel Pineault Post author

      The comma is great for aerating the output, especially for multi-line output.

      Yes, you can use any of the character constants: vbTab, vbNullChar, vbNullString, vbCrLf, vbNewLine, … or string functions … That’s the beauty of Debug.Print, you can output pretty much anything and it gets output right there in front of you!

  2. amen

    Good Morning Sir
    I need your help to show these output with debug.print method in Excel VBA

    1- I would like to show the multiplication table like this by using debug.print method
    ————————————————————————————

    2×1 = 2 3×1 = 3 4×1 = 4
    2×2 = 4 3×2 = 6 4×2 = 8
    2×3 = 6 3×3 = 9 4×3 = 12
    2×4 = 8 3×4 = 20 4×4 = 16
    2×5 = 10 3×5 = 15 4×5 = 20

    5×1 = 5 6×1 = 6 7×1 = 7
    5×2 = 10 6×2 = 12 7×2 = 14
    5×3 = 15 6×3 = 18 7×3 = 21
    5×4 = 20 6×4 = 24 7×4 = 28
    5×5 = 25 6×5 = 30 7×5 = 35

    2- Two dimensional array that show this output by using debug.print method
    ————————————————————————-

    The user will enter the name and Note1,Note2 and Note3.
    After the calculation of the total will be done by row and by column

    Name Note1 Note2 Note3 Total
    ————– —— —— —— —–
    Chuck Norris 24 56 10 90
    Lionel Messi 8 4 9 21
    Christiano Ronaldo 45 96 7 148

    ———————–
    Grand Total 73 156 26

    3- Show Matrix form that show this output by using debug.print method
    ——————————————————————-

    Element of matrix A Element of Matrix B

    12 10 4 9 12 4 15
    6 2 15 12 6 10 5
    9 11 12 2 20 3 13

  3. amen

    There is the code
    1+++++++++++++++++++++
    Sub MultiShow()
    Dim iRow As Integer, jCol As Integer

    For iRow = 2 To 7
    Debug.Print
    For jCol = 1 To 5
    Debug.Print iRow & “x” & jCol & ” = ” & iRow * jCol
    Next
    Next
    End Sub
    2+++++++++++++
    Option Explicit
    Option Base 1

    Sub ArrayJDJ()
    Const nbofClass As Byte = 3
    Const NumStudents As Byte = 3
    Const Tabo As String = ” ”
    Const Dash As String = “-”
    Const NberOfDash = 23

    Dim ScoreArray(1 To NumStudents, 1 To nbofClass)
    Dim AverageArray(1 To NumStudents)
    Dim NameArray(1 To NumStudents) As String

    Dim AvrgPot As Double, SumOfScores As Double
    Dim StudentCount As Integer, ScoreCount As Integer, DashCount As Integer
    Dim i As Integer, j As Integer, Namepot As String

    For StudentCount = 1 To NumStudents

    Debug.Print
    NameArray(StudentCount) = InputBox(“Name of student # ” & StudentCount & ” : “)
    ‘Debug.Print “Score of student ” & NameArray(StudentCount) & ” : ”

    For ScoreCount = 1 To nbofClass
    ScoreArray(StudentCount, ScoreCount) = InputBox(“Enter score for class # ” & ScoreCount & ” : “)
    ‘Debug.Print “Enter score for class # ” & ScoreCount & ” : ” & ScoreArray(StudentCount, ScoreCount)
    Next
    Next

    ‘Display results
    Debug.Print
    Debug.Print Tabo; ” Student Name # “; Tabo; ” Notes”

    For DashCount = 1 To NberOfDash
    Debug.Print Dash;
    Next

    Debug.Print
    Dim k As Long

    For StudentCount = 1 To NumStudents
    k = 15 – Len(NameArray(StudentCount))
    For ScoreCount = 1 To nbofClass
    Debug.Print Space(15); NameArray(StudentCount) & Space(k); AverageArray(StudentCount); Space(5); ScoreArray(StudentCount, ScoreCount)
    Next
    Next

    Debug.Print Tabo
    For DashCount = 1 To NberOfDash
    Debug.Print Dash;
    Next
    Debug.Print
    Debug.Print ” Press ENTER to continue …… ”

    End Sub
    3+++++++++++++
    Option Explicit
    Option Base 1

    Sub MATRIXO()
    Dim A(1 To 9) As Integer, B(1 To 12) As Integer
    Dim i As Integer

    ‘For 3×3 Matrix A
    For i = 1 To 9
    A(i) = InputBox(“Element # ” & i & ” : “)
    Next

    ‘For 3×4 Matrix B
    For i = 1 To 9
    B(i) = InputBox(“Element # ” & i & ” : “)
    Next

    ‘Output
    For i = 1 To 9
    Debug.Print A(i);
    Next

    End Sub