VBA JOIN

Last Updated :

-

Blog Author :

Edited by :

Reviewed by :

Table Of Contents

arrow

Excel VBA JOIN Function

As the name suggests, one may use the VBA JOIN function to combine an array of substrings with the specified delimiter. If we do not specify any delimiter, it takes ā€˜spaceā€™ as a default delimiter character. It does the same work as the Concatenate function in Excel, except we only have to specify the delimiter character once. In the Concatenate function, we have to specify the delimiter character every time between every two strings.

The syntax of the function is

VBA Join Syntax

As we can see, the function takes two arguments and returns a string. Arguments are:

  1. SourceArray: We need to specify or reference an array of substrings joined.
  2. Delimiter: The delimiter is used to separate each substring when creating the resultant string. As this is an optional argument, if we omit it, the delimiter is set to be a space ā€ ā€œ.

The VBA SPLIT function is the opposite of the VBA JOIN function.

VBA JOIN

Examples of VBA Join Function

Below are examples of the Join function in Excel VBA.

VBA Join - Example #1

Suppose we want to join the first (Ramesh), middle (Kumar), and last name (Mishra).

VBA join Example 1

Steps would be:

  • First, we need to open the Visual Basic Editor. We can do the same by clicking on the ā€˜Visual Basicā€™ command in the ā€˜Codeā€™ group under the 'Developer' tab excel or using use the excel shortcut key Alt+F11.
Developer Tab
  • Insert the module by right-clicking on ā€œsheet 1ā€ and choosing the ā€˜Insertā€™ command from the contextual menu, and then choose ā€˜Moduleā€™ to insert.
VBA join Example 1-1
  • Create a subroutine named ā€˜JoiningNameā€™.

Code:

Sub JoiningName()

End Sub
VBA join Example 1-2
  • Use the JOIN function as follows.

Code:

Sub JoiningName()

Range("D2").Value = Join(Array("Ramesh", "Kumar", "Mishra"))

End Sub
VBA join Example 1-3

We can see that we have used the ARRAY function to provide SourceArray to the JOIN function and skipped to specify the delimiter character, so ā€˜spaceā€™ would be the default character. The processed value of the JOIN function will be written in cell D2 when we execute this code using the F5 key or manually.

VBA Join Example 1-4

VBA Join - Example #2

Suppose we want to create various Excel files with the item name containing sales only for that item.

VBA join Example 2
  • Open the Visual Basic Editor using the shortcut key Alt+F11.
  • Right-click on the ā€˜Sheet1ā€² (Example 2)ā€™ sheet to open the contextual menu and click on ā€˜Insertā€™ to insert a VBA 'Module' in the VBA project.
VBA join Example 2-1
  • Define a subroutine named ā€˜CreateItemSoldFilesā€™.

Code:

Sub CreateItemSoldFiles()

End Sub
VBA join Example 2-2
  • We need to set a reference to the ā€˜Microsoft Scripting Runtimeā€™ object library using the Tools menu -> Referencesā€¦ command, as we will use some code (objects), which will not work if we do not include this object library.
VBA join Example 2-3
VBA join Example 2-4
  • Now, we will declare all the variables.

Code:

Dim FSO As New Scripting.FileSystemObject
VBA join Example 2-5

The above FSO variable gives access to the VBA FileSystemObject. After binding, we can use functions like BuildPath, CopyFile, CreateTextFile, etc.

  • The next statement creates a TextStream object. Through the TextStream object, we can read from or append to the original file.

Code:

Dim FSO As New Scripting.FileSystemObject
Dim ts As Scripting.TextStream
VBA join Example 2-6.png
  • We will declare more variables. ā€˜rā€™ is for holding rows in the range, ā€˜fsā€™ is for storing the final joined string, ā€˜colsā€™ for storing numbers of columns in the range, ā€˜FolPathā€™ for storing the path of the folder so that we can save the files in the folder and ā€˜Items_Soldā€™ for storing various item names to create a file with these names.

Code:

Dim r As Range
Dim fs As String
Dim cols As Integer
Dim FolPath As String
Dim Items_Sold As String
VBA join Example 2-7
  • We will define the following statement to count the total number of columns in the range.

Code:

cols = Range("A1").CurrentRegion.Columns.Count
VBA join Example 2-8

This statement will first select the current region for cell A1 and then count the total number of columns in the current region.

  • We will write the following statements for assigning the variable ā€˜FolPathā€™ a path using the VBA ENVIRON function and Concatenation Operator.

Code:

FolPath = Environ("UserProfile") & "DesktopItems_Sold"
If Not FSO.FolderExists(FolPath) Then FSO.CreateFolder FolPath
VBA join Example 2-9

The second statement will create the folder if the folder does not exist in the same location.

  • This code will assign the values of the B column one by one to ā€˜Items_Sold.ā€™ We have used the ā€˜OFFSET functionā€™ to get the reference of the cell in the B column as the currently selected cell is in column A.

Code:

Items_Sold = r.Offset(0, 1).Value
VBA join Example 2-10
  • The following bordered statement will open the files with names stored in the ā€˜Items_Soldā€™ variable one by one in appending mode (it will append the new values at last).

Code:

Set ts = FSO.OpenTextFile(FolPath & "" & Items_Sold & ".xls", ForAppending, True)
Example 2-11

We have used the Concatenate operator with variables ā€˜FolPathā€™ and ā€˜Items_Soldā€™ and static values (ā€œā€ andā€.xlsā€) to create file names for excel files.

  • We need to remember that the VBA JOIN function takes only a one-dimensional array as SourceArray To convert the rows into a one-dimensional array, we need to use Application.Transpose method two times.

Code:

fs = Join(Application.Transpose(Application.Transpose(r.Resize(1, cols).Value)), vbTab)
Example 2-12

We have used the Resize method of range object to resize the range to the width of several columns in the range.

As a delimiter, we have used the ā€˜vbTabā€™ keyword so that it would fill values in different cells.

  • As we have stored the processed value of the JOIN function into the ā€˜fsā€™ variable, we will write the fsā€™s values into new lines of VBA created Excel files for every row in our original file from row number 2 to the last row (in our case it is 350th row).
Example 2-13
  • Before ending the loop, we will close the file. The code would be as shown in the screenshot.
Example 2-14

We have written the full code now.

Code:

Sub CreateItemSoldFiles()

Dim FSO As New Scripting.FileSystemObject
Dim ts As Scripting.TextStream

Dim r As Range
Dim fs As String
Dim cols As Integer
Dim FolPath As String
Dim Items_Sold As String

cols = Range("A1").CurrentRegion.Columns.Count

FolPath = Environ("UserProfile") & "DesktopItems_Sold"
If Not FSO.FolderExists(FolPath) Then FSO.CreateFolder FolPath

For Each r In Range("A2", Range("A1").End(xlDown))

Items_Sold = r.Offset(0, 1).Value
Set ts = FSO.OpenTextFile(FolPath & "" & Items_Sold & ".xls", ForAppending, True)
fs = Join(Application.Transpose(Application.Transpose(r.Resize(1, cols).Value)), vbTab)
ts.WriteLine fs

ts.Close
Next r

End Sub

To execute the code, we will press F5. Then, we can see that it created a folder named ā€˜Items_Soldā€™ with the help of VBA code on the desktop.

Example 2-15

In the folder, there are 7 unique files created with the names of the item. Therefore, we can find details about only that particular item in files.

Example 2-16

Laptop.xls

Example 2-17

Things to Remember About VBA JOIN Function

  • The SourceArray must be a one-dimensional array. Therefore, we cannot refer to an individual cell, as this will create multiple multi-dimensional arrays.
  • Suppose we specify a zero-length string (ā€œā€) as a delimiter, all items in the array concatenated with no delimiters.