Google Groups no longer supports new usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What does the semi-colon do in vba code?

3,655 views
Skip to the first unread message

Russell

unread,
7 Nov 2009, 14:00:0107/11/2009
to
I ran across this line of code, which uses the semicolon instead of the
ampersand concatenation character. The only difference I could tell is the
semicolon adds an extra space to the output. I've been using VBA for a while
now and I was surprised to see the semi-colon used as a concatenation
character. Are there any other differences besides the extra space?

Here's a sample Sub

Public Sub Semicolon_test()

Dim intNumColumns As Integer

intNumColumns = 1

'Semi-colon example
Debug.Print "The list box contains"; intNumColumns; IIf(intNumColumns =
1, "column", "columns"); " of data."

'Same thing except the semicolon is replaced with the ampersand and the
spacing is modified.
Debug.Print "The list box contains " & intNumColumns & IIf(intNumColumns
= 1, " column", " columns") & " of data."

End Sub

Marshall Barton

unread,
7 Nov 2009, 15:24:3507/11/2009
to
Russell wrote:


The semi-colon in Print is a value separator (without a
space or starting new line).

The alternative is to use a comma, which uses some strange
idea of a column to separate the values (kind of like a
tab?).

Concatenation is a different issue, even if the result looks
similar.

--
Marsh
MVP [MS Access]

OssieMac

unread,
8 Nov 2009, 23:54:0108/11/2009
to
Hi Russell,

The semicolon is not so much a concatenation character as a delimiter
between values and when used with debug.print that is exactly what it does.
It is similar to multiple Debug.print commands except it does not insert a
line feed.

Try using a a comma in lieu of the semicolon and it is more like a tab.
(extra space)

However, the ampersand joins all of the strings into one string and can be
used in code to join several strings and then address them as one string.

Example using your parameters.
strConcatenated = "The list box contains " & intNumColumns &
IIf(intNumColumns _


= 1, " column", " columns") & " of data."

Debug.Print strConcatenated

Now some more info on your example.

intNumColumns = 1

This is actually +1 and the leading space produced is actually the missing +
sign. You can't enter the + sign in your code because VBA intellisence
removes it but try using -1 in lieu of 1 and run the code again and you will
see what I mean.


--
Regards,

OssieMac

Sir Joe

unread,
4 Sept 2012, 06:07:5404/09/2012
to
On Monday, 9 November 2009 05:54:01 UTC+1, OssieMac wrote:

> Try using a a comma in lieu of the semicolon and it is more like a tab.
> (extra space)

thanks! this worked very well for me!
Sir Joe
0 new messages