The VBA Join function joins together an array of substrings and returns a single string.
The syntax of the function is:
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 " ". |
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"
' The variable fullName is now set to "John Paul Smith"
names(1) = "Paul" names(2) = "Smith" fullName = Join( names ) |
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.
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:"
' The variable fullPath is now set to "C:\My Documents\DataFiles"
dirs(1) = "My Documents" dirs(2) = "DataFiles" fullPath = Join( dirs, "\" ) |
After running the above VBA code, the variable fullPath is set to the string "C:\My Documents\DataFiles".