How to print two dimensional array in Immediate window in VBA?

Qbik picture Qbik · Jan 11, 2013 · Viewed 75.1k times · Source

How to print two dimensional array in Immediate window in VBA ? Does it exist any generic method for doing this ? Some method for ploting one row of array per line in Immediate window could solve this problem, because then only thing to do is to loop this code for each line of array.

Answer

johny why picture johny why · Jul 12, 2016

If this is for debugging purposes, it's not convenient to write a macro to view the contents of the array during program execution. Might even cause problems.

For debugging during program execution, you'll want a code-free method you can use across all VB projects, to spy the contents of your array.

  1. In VBA IDE, click View menu > Locals Window
  2. In Local pane, find the array-name. enter image description here
  3. Expand the nodes to find your values. The nodes will differ, depending on the type of array.

In this example, "aLookList" is a variant array. The values under "Value2" come from a range.

enter image description here

enter image description here

Another answer here suggests using the Watch pane. This is similar to my answer, but poster did not explain that you can spy the entire array (all cells) by simply adding the array name to Watch. Then , drill down nodes. The advantage of the Locals Window over the Watch Window, is that with Locals pane, you do not have to manually add the array to the pane, it's already there. So it's a bit less effort.