ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   How to concatenate adjacent cells in a range without using &? (https://www.excelbanter.com/excel-worksheet-functions/50653-how-concatenate-adjacent-cells-range-without-using.html)

Ark

How to concatenate adjacent cells in a range without using &?
 
I would like to know if there is a way to concatenate multiple cells in a row
without having to use concatenate with "," or "&" ?

i.e. is there something like concatenate(A1:F1)?


Biff

How to concatenate adjacent cells in a range without using &?
 
Hi!

is there something like concatenate(A1:F1)?


No, no built in way to do that. Maybe something like that in the upcoming
version.

Here's a UDF that I found that does it: (don't know the author to
acknowledge)

Function Concatall(rng As Range) As String
Dim cell As Range
For Each cell In rng
Concatall = Concatall & cell.Text & " "
Next
End Function

Put it in a General module.

This concatenates a range and uses a space as a separator. If there are
empty cells in the range they are included in the returned string:

Suppose your range is A1:A5:

A1 = 1
A2 = empty
A3 = 3
A4 = 4
A5 = 5

The formula (using the UDF) would be:

=CONCATALL(A1:A5)

The return would be: 1<space 3 4 5

You can get rid of the spaces by using this formula:

=SUBSTITUTE(concatall(A1:A5)," ","")

The return would be: 1345

Or, you could get rid of the space AND change the separator to a comma by:

=SUBSTITUTE(TRIM(concatall(A1:A5))," ",", ")

The return would be: 1,3,4,5

Biff

"Ark" wrote in message
...
I would like to know if there is a way to concatenate multiple cells in a
row
without having to use concatenate with "," or "&" ?

i.e. is there something like concatenate(A1:F1)?




GRM via OfficeKB.com

How to concatenate adjacent cells in a range without using &?
 
Ark wrote:
I would like to know if there is a way to concatenate multiple cells in a row
without having to use concatenate with "," or "&" ?

i.e. is there something like concatenate(A1:F1)?


Yes there is a function if you prefer. Syntax is CONCATENATE(A1,B1,C1) not A1:
C1

But you'll find using & a lot quicker

Gord Dibben

How to concatenate adjacent cells in a range without using &?
 
Ark

You will have to use a User Defined Function.

Function ConCatRange(CellBlock As Range) As String
Dim Cell As Range
Dim sbuf As String
For Each Cell In CellBlock
If Len(Cell.text) 0 Then sbuf = sbuf & Cell.text & ","
Next
ConCatRange = Left(sbuf, Len(sbuf) - 1)
End Function

Usage is: =concatrange(A1:F1)

Note you can change the comma separator to a space or any combination of
deleimiter.

If you prefer, you could use a macro to do same thing.

Sub ConCat_Cells()
Dim x As Range
Dim y As Range
Dim z As Range
Dim w As String
Dim sbuf As String
On Error GoTo endit
w = InputBox("Enter the Type of De-limiter Desired")
Set z = Application.InputBox("Select Destination Cell", _
"Destination Cell", , , , , , 8)
Application.SendKeys "+{F8}"
Set x = Application.InputBox _
("Select Cells...Contiguous or Non-Contiguous", _
"Cells Selection", , , , , , 8)
For Each y In x
If Len(y.text) 0 Then sbuf = sbuf & y.text & w
Next
z = Left(sbuf, Len(sbuf) - 1)
Exit Sub
endit:
MsgBox "Nothing Selected. Please try again."
End Sub


Gord Dibben Excel MVP

On Sat, 15 Oct 2005 21:14:02 -0700, Ark wrote:

I would like to know if there is a way to concatenate multiple cells in a row
without having to use concatenate with "," or "&" ?

i.e. is there something like concatenate(A1:F1)?



Ron Rosenfeld

How to concatenate adjacent cells in a range without using &?
 
On Sat, 15 Oct 2005 21:14:02 -0700, Ark wrote:

I would like to know if there is a way to concatenate multiple cells in a row
without having to use concatenate with "," or "&" ?

i.e. is there something like concatenate(A1:F1)?


You could use a UDF.

The one below puts n Spaces between each string. It will handle a contiguous
cell range, non-contiguous cell ranges, or mixed ranges and strings as an
argument. For example:

=setstring(1,A1:D1,G1:H1,"the end")

would be a valid function call.

========================
Function SetString(SpacesBetween As Integer, _
ParamArray rg() As Variant) As String

'by Ron Rosenfeld

Dim c As Variant
Dim i As Long

For i = 0 To UBound(rg)
Select Case VarType(rg(i))
Case Is = vbArray + vbVariant
For Each c In rg(i)
SetString = SetString & Space(SpacesBetween) & Trim(c.Text)
Next
Case Is = vbString
SetString = SetString & Space(SpacesBetween) & Trim(rg(i))
End Select
Next i

SetString = Trim(SetString)

End Function
====================


--ron


All times are GMT +1. The time now is 03:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com