How to Concatenate Strings, Integers, Variables, Cells in VBA

In VBA, you may concatenate values, strings, variables, and Excel data by using the ‘&’ operator.

Concatenation in VBA

In VBA, you may concatenate values, strings, variables, and Excel data by using the ‘&’ operator.

An example of concatenating two string variables

In the first example, we have two string variables.

The task is to combine these two strings and display them in the message box.

VBA Code:

Output:

VBA-concatenate-string

Combining variable with text example

We have one string variable and some text in the message box that is concatenated by using the ‘&’ operator:

The code:

Result:

VBA-concatenate-text

Adding a new line as a concatenation between variables

You may also add a new line by using its constant to make the text clearer after concatenation.

The following example uses a new line constant

For adding a new line we used vbCrLf constant.

VBA code:

Output:

VBA-concatenate-newlin

Note: You may also use vbNewLine constant instead of vbCrLf.

Concatenating string and integer type variable

Let us see what we get as we concatenate some text, a string variable, and an integer type variable – combined all in a message box:

VBA code:

Output:

VBA-concatenate-number

An example of concatenating Excel cells data

In the following example, we will concatenate the data of Excel cells. For that we have the following sample sheet:

VBA-Excel-sample-conca

We will combine the text as follows:

Code:

Result:

VBA-concatenate-cells

Concatenating range of cells

You may also specify a range of cells and combine by using different techniques. One of the techniques is using the For Each loop.

See an example below where we will concatenate “Product Names” from B2 to B7 cells and display all in a MsgBox:

The code:

Result:

VBA-Excel-cells-range