Concatenate Text in an Array Formula

terrygtx

New Member
Joined
Nov 18, 2005
Messages
3
Hi all,

This is probably pretty easy, but I have searched with no luck.

What I would like to do is to concat a range of cells containing text values into a comma delimited list in a single cell.

i.e.

A1 = one
A2 = two
A3 = three
A4 = four

the resulting cell would be = one,two,three,four

Then if row A2 was deleted the result would be = one,three,four

TIA,

t
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hello, and welcome to the board! 8-)

The only good way I can think to do this is with a VBA UDF, i.e.,

Code:
Public Function foo(ByRef rngIn As Range) As String
Dim tmpStr As String
Let tmpStr = Join(WorksheetFunction.Transpose( _
    rngIn), ", ")
Do While InStrB(1, tmpStr, ", ,", vbBinaryCompare)
    Let tmpStr = Replace$(tmpStr, ", ,", ",")
Loop
Let foo = tmpStr
End Function
Book1
ABCD
111, 4
2
3
44
Sheet1 (2)


There's actually a good reason, here, not to use .Value on the Range, if you're using xl 2000. :wink:

This has been given limited testing... Good hunting. :)
 
Upvote 0
I assume that when you say "row A2 is deleted" you mean that the cell contents are deleted.

Four 4 cells as per your example you could use

=SUBSTITUTE(IF(A1="","",","&A1)&IF(A2="","",","&A2)&IF(A3="","",","&A3)&IF(A4="","",","&A4),",","",1)

but obviously this gets a bit cumbersome, if not completely unworkable, if you have a much bigger range.

In that situation I think you'd have to use code, as Nate suggests, or you could use MCONCAT function from MOREFUNC add-in and use

=SUBSTITUTE(MCONCAT(IF(A1:A4<>"",","&A1:A4,"")),",","",1)

confirmed with CTRL+SHIFT+ENTER

(untested)
 
Upvote 0
Thank you both for the prompt reply.

I tried barry's method and it works great. Thank you again, very much.

As for your initial assumption, no. I meant the whole row (or column - which ever) is deleted - not just the cell contents. That was why I wanted an array formula. I wanted to specify the end points only to accomodate changes such as addtions and deletions and have the comma delimited list automatically refresh.

I suppose the drawback to this method over the one NateO proposed it that this will only work on a machine with the MOREFUNC add-in. Is that correct? In other words, if someone attempts to open this file on another machine (without morefunc), what will happen?

t
 
Upvote 0
terrygtx said:
I suppose the drawback to this method over the one NateO proposed it that this will only work on a machine with the MOREFUNC add-in. Is that correct? In other words, if someone attempts to open this file on another machine (without morefunc), what will happen?
t

Yes, you need MOREFUNC installed or MCONCAT won't be recognised so Nate's proposal may suit you better
 
Upvote 0
Yes, but will it error right off the bat? Or will it display the last calculated value from my machine and only error if a cell in the range is changed?

Sorry for asking such a basic question.
 
Upvote 0
Terry,

I don't think that's a basic question, I didn't know the answer myself :-(

I believe the last calculated value will be shown until the range is changed or recalculation takes place for any other reason
 
Upvote 0
barry houdini said:
....
=SUBSTITUTE(MCONCAT(IF(A1:A4<>"",","&A1:A4,"")),",","",1)

confirmed with CTRL+SHIFT+ENTER

(untested)
Hi barry houdini:

Unless I am missing something here, using MCONCAT from the MoreFunc Add-in, wouldn't the following formula work ...

=MCONCAT(A1:A4,",")

the formula should work even if the cell contents of A2 get deleted or the entire row gets deleted.

What do you think?
 
Upvote 0
Yogi,

Good call, that appears to be the case.

I had thought that MCONCAT would give multiple separators if there were multiple blanks, hence my unnecessarily convoluted construct.

has MCONCAT changed or is it just my ignorance? :cry:
 
Upvote 0
barry houdini said:
Yogi,

Good call, that appears to be the case.

I had thought that MCONCAT would give multiple separators if there were multiple blanks, hence my unnecessarily convoluted construct.

has MCONCAT changed or is it just my ignorance? :cry:
Hi barry:

Can't say ... I am not uptodate with the latest stuff ... I am still using EXCEL 97!
 
Upvote 0

Forum statistics

Threads
1,226,222
Messages
6,189,708
Members
453,566
Latest member
ariestattle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top