Home > Quick Tips > Easily display array values in a Msgbox for debugging

Easily display array values in a Msgbox for debugging

Happy New Year!  I hope you enjoyed some time off with family and friends for the holidays.  From time to time, I will post some shorter topics that are more like quick tips than full articles.  I figured everyone is just getting back to work this week and probably has a lot going on so this is a good time for a short and simple topic.  In this article, I will show a simple way to display array values in MsgBox.  This is a very handy debugging tool I sometimes use when I need to check array values as a program is running.  And best of all, it only requires one line of code.

Before I show how it’s done, I want to mention that this technique only works when the array is one dimensional and  holds value data types such as strings, numbers or booleans (true/false).  It will not work if the array references object data types such as drawings, geometry or parameters in CATIA.

How it’s done

The Visual Basic language has some very useful built in functions to work with arrays.  One of them is called Join and it is used to take all the values in a one dimensional array and join them together into a single string.  The function also allows you to specify an optional delimiter character that will get inserted in between each value.  The result can then be easily displayed in a MsgBox.

Be smart when choosing delimiter characters

Most of the time, I use either a comma and space together or vbCrLf (stands for carriage return line feed) as the delimiter character depending on the data I am trying to display.  For example, if the array holds the three coordinates of a point, I typically use a comma and a space as the delimiter so the three values are comma separated on a single line.  On the other hand, if the array holds the position matrix of a product you might choose vbcrlf as the delimiter because that matrix holds 12 values.  This way, each value will be on a separate line and might be easier to read.

Example 1

Assume the coordinates of a point were previously measured and assigned in a 3 value array called varCoords.  Maybe your program is not working right or you just need to check those coordinates.  Simply insert this line after the point was measured to see the values.

MsgBox "Coordinates of point: " &Join(varCoords, ", "), 0, "Debug"

Notice that I used a comma and a space as the separator characters to produce an easy to read comma separated string.  At runtime, you will see a message displayed something like this,

Example 2

Assume the position of a product in an assembly was obtained and stored in a 12 value array called varMatrix.  In case you have never dealt with this sort of position array, this is what each value represents: 0-2 represents the X direction vector, 3-5 represents the Y direction vector, 6-8 represents the Z direction vector and 9-11 is the origin.  To see these values at runtime just insert this line of code:

MsgBox "Product position matrix: " &vbCrLf &vbCrLf &Join(varMatrix, vbCrLf), 0, "Debug"

Note that this time, I used a carriage return (vbCrLf) as the separator character to produce a new line between each value.  At runtime, you will see a message displayed something like this,

When to use

To be honest, most of the time I write vb code in the built in CATIA VBA editor.  In VBA, there is a very capable set of debugging tools that allow you to set break points and use watch and immediate windows to easily check values.  If you haven’t tried the VBA editor you are really missing out – if you get some free time, play around with it a bit.  I am working on an upcoming article right now discussing the advantages of using the VBA editor in CATIA, so watch for that one soon.

Although I like to work in VBA, there are some situations where it just isn’t a realistic option.  In these cases, using a MsgBox as shown can be a very useful debugging technique.  A great example is when you develop KBE models using the knowledgeware workbenches in CATIA.  Often there is a need to apply vb code inside various relations which get stored in the specification tree.  Unfortunately, when you edit your code inside these features, the code editors that are provided have very minimal functionality especially for debugging.  This leaves you with few options besides Msgbox to debug your code.

Also, as a side note, keep in mind that the Join function has many other uses in your programming projects besides what I showed here.  There is also a closely related function called Split that converts a string having some delimiter into an array.  You can really do some powerful operations using these two functions.


Be aware that the Msgbox function can only display up to 1,024 characters and it will also only display a limited number of lines.  This means arrays with many values or many characters may get cut off but the above technique should work fine for most arrays you will probably care to display.

Please take a moment to rate this article…Just click the stars up near the title.

Add to: del.icio.us del.icio.us, StumbleUpon StumbleUpon, Digg Digg, Google Google

  1. Karteek
    January 13, 2010 at 11:55 am

    Hi Mike,

    This was really very useful and a great learning.
    I have recently started learning macro creation in V5.

    Thank you,
    Karteek Manchala

    June 14, 2010 at 11:36 pm


    For debuging my code in VB or VBA, I use the instruction
    Debug.Print (what i want to be displayed)

    And then in the IDE i just open (Ctrl+G)the debug window, and during the program running the debug window displays (what i want to be displayed)

    I prefer this way because debugging with Msgbox makes me clicking in the Ok button many times. And as you said debugging in debug windw has no limitation in length of characters or variables.

    • June 15, 2010 at 5:49 am

      You are absolutely right, the immediate window is a great alternative to a msgbox when you are working in a development environment. Nice tip!

  3. Shwetha
    September 16, 2011 at 9:26 pm

    Hello i have a written a code to display array values in single msgbox.. code is below. But in the msgbox i am unable to see the output values.. Please let me know what is the issue.

    Dim myarray(100)
    For i=50 to 80
    if i mod 2 0 then
    myarray(i)= i
    End If
    MsgBox “Even values are : ” & vbCrLf & vbCrLf & Join(myarray, vbCrLf)

    • September 16, 2011 at 10:06 pm

      Your array bound is 100 and your for loop starts at 50 so the first 50 lines are blank when you display the array. Try i = 0 to 30 or something similar and you will see the values. Also, the MsgBox function is not meant to display 100 lines..it will extend way off the bottom of the screen.

  4. Jerry
    May 20, 2013 at 7:35 am

    This was great for learning about array for me.

  1. No trackbacks yet.

Leave a Reply to Jerry Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: