The VBA Join Function

Related Function:
VBA Split

Description

The VBA Join function joins together an array of substrings and returns a single string.

The syntax of the function is:

Join( SourceArray, [Delimiter] )

Where the function arguments are:

SourceArray - The array of substrings that you want to join together.
[Delimiter] -

The delimiter that is used to separate each of the substrings when making up the new string.

If omitted, the [Delimiter] is set to be a space " ".


VBA Join Function Examples

Example 1

The following VBA code joins together the strings "John", "Paul" and "Smith".

' Join together the strings "John", "Paul" and "Smith".
Dim fullName As String
Dim names( 0 to 2 ) As String
names(0) = "John"
names(1) = "Paul"
names(2) = "Smith"
fullName = Join( names )
' The variable fullName is now set to "John Paul Smith"

After running the above VBA code, the variable fullName is set to the string "John Paul Smith".

Note that the [Delimiter] argument has been omitted from the function, and so the default character (a space) is used.


Example 2

The following VBA code joins together the strings "C:", "Users" and "My Documents" with the backslash character used as the delimiter.

' Join together the strings "C:", "My Documents" and "DataFiles".
Dim fullPath As String
Dim dirs( 0 to 2 ) As String
dirs(0) = "C:"
dirs(1) = "My Documents"
dirs(2) = "DataFiles"
fullPath = Join( dirs, "\" )
' The variable fullPath is now set to "C:\My Documents\DataFiles"

After running the above VBA code, the variable fullPath is set to the string "C:\My Documents\DataFiles".