**Please Note: **
This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), *this tip may not work for you*. For a version of this tip written specifically for later versions of Excel, click here: Concatenating Ranges of Cells.

Excel provides one workbook function and one operator that both have the same purpose—to combine strings into a longer string. The CONCATENATE function and the ampersand (&) operator have essentially the same purpose.

Many people use the ampersand operator in preference to the CONCATENATE function because it requires less typing, but CONCATENATE would become immensely more valuable if it would handle a range of cells. Unfortunately it does not, but you can create your own user-defined function that will concatenate every cell in a range very nicely. Consider the following macro:

Function Concat1(myRange As Range, Optional myDelimiter As String) Dim r As Range Application.Volatile For Each r In myRange Concat = Concat & r & myDelimiter Next r If Len(myDelimiter) > 0 Then Concat = Left(Concat, Len(Concat) - Len(myDelimiter)) End If End Function

This function requires a range and provides for an optional delimiter. The last "If" statement removes the final trailing delimiter from the concatenated string. With the CONCAT1 function, cells can be added and deleted within the range, without the maintenance required by CONCATENATE or ampersand formulas. All you need to do is call the function in one of the following manners:

=CONCAT1(C8:E10) =CONCAT1(C8:E10,"|")

The second method of calling the function uses the optional delimiter, which is inserted between each of the concatenated values from the range C8:E10. There is a problem with this, however: If a cell in that range is empty, then you can end up with two sequential delimiters. If you prefer to have only a single delimiter, then you need to make one small change to the function:

Function Concat2(myRange As Range, Optional myDelimiter As String) Dim r As Range Application.Volatile For Each r In myRange If Len(r.Text) > 0 Then Concat = Concat & r & myDelimiter End If Next r If Len(myDelimiter) > 0 Then Concat = Left(Concat, Len(Concat) - Len(myDelimiter)) End If End Function

*Note:*

If you would like to know how to use the macros described on this page (or on any other page on the *ExcelTips* sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (3062) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: **Concatenating Ranges of Cells**.

**Program Successfully in Excel!** John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out *Excel 2013 Power Programming with VBA* today!

If you lose your place on the screen quite often, you might find it helpful to have not just a single cell highlighted, ...

Discover MoreCopying information between two instances of Excel is different than copying information between two worksheets opened in ...

Discover MoreDo you need to both transpose and link information you are pasting in a worksheet? It isn't as impossible to do as it ...

Discover More**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

2016-03-01 18:40:30

strChrisP

One other (somewhat trivial) error to correct: If the range selected is currently empty and a delimiter is specified, then the block below produces an error:

If Len(myDelimiter) > 0 Then

Concat = Left(Concat, Len(Concat) - Len(myDelimiter))

End If

This needs to be surrounded thus:

If Len(Concat) > 0 Then

If Len(myDelimiter) > 0 Then

Concat = Left(Concat, Len(Concat) - Len(myDelimiter))

End If

End If

2016-02-21 00:41:19

Alex

=(A86&","A87&","&A88&","A89&","&A90&","

&A193&","&A194&","&A195&","&A196&","&A197&","&A198)

How can I use your function to achieve this? I mean, where can I put your code? and what to type in cell A1?

Than you.

2014-12-24 11:48:50

Vikas Gautam

Syntax

=JoinC( Range _ Array, Dilimiter as String )

=JoinR( Range _ Array, Dilimiter as String )

For Col ConCatenation

Select Code copy to clipboard

Function JoinC(ByRef x As Variant, ByRef Delim As String) As String

'******* Developed by Vikas Gautam *******

'*****Valued Contributor at ExcelForum.com*****

'****For One dimensional Arrays having one Row and many Columns*****

With Application

SourceArray = .Transpose(x)

Temp = Replace(Delim & Join(SourceArray, Delim) & Delim, Delim & Delim, Delim)

Do While InStr(1, Temp, Delim & Delim, 1) > 0

Temp = Replace(Temp, Delim & Delim, Delim)

Loop

End With

If Right(Temp, 1) = Delim And Left(Temp, 1) = Delim Then _

JoinC = Mid(Temp, 2, Len(Temp) - 2)

ElseIf Left(Temp, 1) = Delim Then

JoinC = Mid(Temp, 2, Len(Temp))

ElseIf Right(Temp, 1) = Delim Then

JoinC = Mid(Temp, 1, Len(Temp) - 1)

End If

End Function

For Row Concatenation

Select Code copy to clipboard

Function JoinR(ByRef x As Variant, ByRef Delim As String) As String

'******* Developed by Vikas Gautam *******

'*****Valued Contributor at ExcelForum.com*****

'****For One dimensional Arrays having one Row and many Columns*****

With Application

SourceArray = .Transpose(.Transpose(x))

Temp = Replace(Delim & Join(SourceArray, Delim) & Delim, Delim & Delim, Delim)

Do While InStr(1, Temp, Delim & Delim, 1) > 0

Temp = Replace(Temp, Delim & Delim, Delim)

Loop

End With

If Right(Temp, 1) = Delim And Left(Temp, 1) = Delim Then _

JoinR = Mid(Temp, 2, Len(Temp) - 2)

ElseIf Left(Temp, 1) = Delim Then

JoinR = Mid(Temp, 2, Len(Temp))

ElseIf Right(Temp, 1) = Delim Then

JoinR = Mid(Temp, 1, Len(Temp) - 1)

End If

End Function

2014-09-26 16:48:58

Concat1 = Concat

Add that and the very first code works.

2014-03-19 18:49:13

Michael (Micky) Avidan

Believe me - a small credit to Dave would not hurt...

http://excelribbon.tips.net/T011247_Concatenating_Ranges_of_Cells.html

Michael (Micky) Avidan

“Microsoft® Answers" - Wiki author & Forums Moderator

“Microsoft®” MVP – Excel (2009-2014)

ISRAEL

2014-03-18 11:58:35

Duane Hookom

Public Function Concat1(myRange As Range, Optional myDelimiter As String)

Dim r As Range

Dim Concat As String

Application.Volatile

For Each r In myRange

Concat = Concat & r & myDelimiter

Next r

If Len(myDelimiter) > 0 Then

Concat1 = Left(Concat, Len(Concat) - Len(myDelimiter))

End If

End Function

2014-03-17 09:08:45

NUha

Thx

Nuha

2013-02-05 16:00:28

Jeremy Stein

2013-01-10 22:25:30

xypho

In fact you do not need to declare a superfluous variable (Concat)...

in addition, you could have an optional parameter to include the empty text.

I also like to leave the delimiter empty, but to each their own.

And I prefer using mid rather than left for the removal of superfluous delimiter (which means the delimiter is at the start of the string), one less string operation.

Option Explicit

Public Function Concat(myRange As Range, Optional myDelimiter As String = "", Optional includeEmptyText As Boolean = True)

Dim r As Range

'make sure this function is recalculated when the target range is recalculated

Application.Volatile

'build the concat string

For Each r In myRange

If (includeEmptyText Or Len(r.Text) > 0) Then

Concat = Concat & myDelimiter & r

End If

Next r

'remove the superfluous delimiter

If Len(myDelimiter) > 0 Then

Concat = Mid(Concat, Len(myDelimiter) + 1)

End If

End Function

2012-01-29 11:08:00

Duane Hookom

- concat is not declared which would have been discovered if the module General Declarations included Option Explicit

- Concat2 is not set to the value of concat so the function returns nothing.

I would also set an optional delimiter of ", " since this is the most common.

Function Concat2(myRange As Range, _

Optional myDelimiter As String = ", ")

Dim r As Range

Dim concat As String

Application.Volatile

For Each r In myRange

If Len(r.Text) > 0 Then

concat = concat & r & myDelimiter

End If

Next r

If Len(myDelimiter) > 0 Then

concat = Left(concat, Len(concat) - Len(myDelimiter))

End If

Concat2 = concat

End Function

Got a version of Excel that uses the
menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)?
**This site is for you!** If you
use a later version of Excel, visit
our *ExcelTips* site focusing on the ribbon interface.

**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

Copyright © 2019 Sharon Parq Associates, Inc.

## Comments