When working with Microsoft Excel, there are times you may want to merge text strings in your macros. That’s where Visual Basic for Applications (VBA), the programming language built into Excel, comes into play. The process of merging text strings is called concatenation, and it can be a real time-saver when automating repetitive tasks. Using the ampersand operator, I can easily join multiple strings of text and have the result displayed in a cell, stored in a variable, or even shown in a message box for quick feedback.
In the spirit of sharing knowledge and helping each other out, I’m looking forward to showing you an easy example of how concatenation works in VBA. Whether you’re new to programming or looking for additional resources to get better at Excel, I’ve got some tricks up my sleeve that could make life a little easier for you.
Steps to use VBA to Concatenate
To work with strings and combine them in VBA, I usually:
- Open the Visual Basic Editor from the Developer tab.
- Insert a Module for my macro.
- Start a Sub procedure, let’s call it
Sub vba_concatenate()
. - In my sub, I define two strings inside double quotes, like
"String1"
and"String2"
. - Then, I use an ampersand (
&
) to concatenate them.
Here’s how I do it in code:
Sub vba_concatenate()
Dim myResult As String
myResult = "FirstString" & "SecondString"
Range("A1").Value = myResult ' Assign the concatenated string to a cell
End Sub
This succinctly assigns the concatenated strings to a cell or variable, avoiding any performance issues with long formulas or numerous arguments.
Concatenate with a Delimiter
When I need to merge multiple strings and include something specific, like a dash, between them, I use a delimiter. It’s simple. I just drop in an extra ampersand (&) to add the delimiter like so:
myString = "Puneet" & "-" & "Gogia"
Here, I’ve concatenated “Puneet” and “Gogia” with a dash between them. The trick is all in the ampersands!
Concatenate using Variables
With Excel VBA, I often use the ampersand (&
) to concatenate variables. Here’s a quick snippet:
Dim var1 As String
Dim var2 As String
var1 = "Hello, "
var2 = "World!"
Range("A1").Value = var1 & var2
In this example, var1
and var2
are my string variables. After setting them up with some text, I combine them into one string and then display it in cell A1. It’s clean and straightforward!
Concatenate a Range using VBA
When I need to combine string values from different cells into one, I like using a VBA macro in my Excel workbook. Here’s a neat way to do it using an array to capture the range:
- First, I declare variables for both my range (
rng
) and my resulting string (i
). - Using a
For Each
loop, I traverse each cell in my specified range (let’s sayA1:A10
). - Inside the loop, I use an ampersand
&
to tack on each cell’s value toi
, along with a space for readability. - Finally, I make sure to remove any trailing spaces with
Trim(i)
and set the cell where I want the result (B1
) with this concatenated string.
The result? All the values from A1
to A10
nicely lined up in B1
. And if I want to concatenate cells I’ve selected manually, I can use the same logic—simply replace Set SourceRange = Range("A1:A10")
with Set SourceRange = Selection
. Just like that, no more dragging formulas across cells or typing out long CONCATENATE
functions!
Concatenate Entire Column or a Row
' To join values from column A
Dim myRange As Range
Range("B1") = WorksheetFunction.TextJoin(" ", True, Range("A:A"))
' For concatenating values from row 1
Dim myString As String
Range("B1") = WorksheetFunction.TextJoin(" ", True, Range("1:1"))
- Use TextJoin to efficiently concatenate a column or row
- It lets me include a delimiter like a space (” “) or comma (“,”)
- Set the second argument to True to ignore empty cells
- The output will neatly appear where I specify, like in “B1”