28 May
Posted by Chandoo in Excel Tips, hacks, technology
Excel concatenate() is seriously crippled, it can add 2 or more strings together, as long as they are supplied as separate parameters. This means, when you have a range of cells with text which you want to add up to create a large text, you need to write an ugly looking biggish concatenate() or use ‘&’ operator over and again.
I felt bored enough the other day to write a better concatenate(), one that can accept a range as input and output one text with all the contents of the input range. What more you can use this to delimit the input range with your own favorite character.
For example, if each of the 7 cells in a1:a7 have “a”, “b”, “c”, “d”, “e”, “f”, “g”, if you want to add all of them up using concatenate you would have to write concatenate(a1,a2,a3,a4,a5,a6,a7) which can be painful if you are planning to do this over a large range or something.
Instead, you can use concat(a1:a7) by installing the UDF (User defined function) I have written. Its nothing miraculous or anything, it just does the dirty job of going through the range for you. If you want to delimit the input range with a comma just use concat(a1:a7,",") to get the out of a,b,c,d,e,f,g Just download the concat() UDF excel add-in and double click on it to install it. If you are little weary of installing UDFs / Macros from third parties, copy past the below excel code in a new sheet’s VB editor and save the sheet as an excel addin (.xla extension)
Added on Aug 26, 2008: I have updated the code, copy it again if you have the old one
Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
Dim retVal, dlm As String
retVal = ""
If delim = Null Then
dlm = ""
Else
dlm = delim
End If
For Each cell In useThis
if cstr(cell.value)<>“” and cstr(cell.value)<>” ” then
retVal = retVal & cstr(cell.Value) & dlm
end if
Next
If dlm <> “” Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function
Did you find this useful? Are you looking for some other excel UDFs as well, drop a comment, I am a busy coffee drinker, but between the sips I can whip out ugly looking but functional vb code ![]()
19 Responses
Hypnos
May 29th, 2008 at 8:51 am
1You are a god in Excel!
I bow down to you o master of Excel…
Hypnos
May 29th, 2008 at 8:59 am
2But… how do I use it?
The formula does not appear when i type in ‘=conc…’ in a cell.
Chandoo
May 29th, 2008 at 2:47 pm
3@Hypnos .. thanks alot man
I think you need to save the downloaded xla file in your excel add-in folder (just select save as and use the excel add-in as the file type, the folder will be shown in the save dialog automatically), let me know if this doesnt work.
Hypnos
June 2nd, 2008 at 1:30 pm
4Duh!
It didn’t work, that is why I wrote… give me some credit dude
I used to be in your ITCOM.
Chandoo
June 2nd, 2008 at 2:38 pm
5hmm.. thats tricky, I read that it works that way, it actually did work that way for me, by saving the xla file in the addins folder, i could see and use the formulas. Btw, the formula may not appear when you type =concat… in the cell, but it works nevertheless. Anyways, can you try pasting the code in a module in your sheet and save that sheet as an addin instead… meanwhile I will investigate why this wouldn’t work…
captsri
June 5th, 2008 at 11:51 am
6that was a great lead to me, and thansk a ton. need you help in this problem. in eh above example if a3 has ‘c’ but the actual field length should be 8 how do i make use of the same program to create a fixed length text so that i can export it as *.edi file.
In essence i want to create form a excel sheet an exi file withfixed text format having an option to enter headers
Chandoo
June 5th, 2008 at 1:24 pm
7@capstri .. thanks for the comments..
let me see if I can help you…
In the above code you can change the for loop to something like this to get the desired effect.
For Each cell In useThis
retVal = retVal + cell.Value + rept(” “,8-len(cell.Value))
Next
you can replace the “8″ with whatever fixed lenght you have in mind. Also, I have not tested the above code, you may have to replace the rept() with something else if it throws an error. Let me know so that I will help you if I can…
How to count words in a cell using excel functions | Pointy Haired Dilbert - Chandoo.org
July 8th, 2008 at 3:38 pm
8[...] Concatenate a bunch of cells using simple formula, Generate tag clouds in excel using vba, Master your IFs and BUTs Tags: Analytics, count, excel, [...]
colebro
July 13th, 2008 at 2:40 pm
9When I add/save the xla file to my addins directory ‘C:\Documents and Settings\colebro\Application Data\Microsoft\AddIns’, i double click on the file to install it. When I enter the command =concat(a4:d4) I get the invalid value (#VALUE!). The ‘Excel-Udf-Concat’ addin is checked iin my add-ins available list. So, I copied your excel code and saved it as an xla file in the add-ins directory, made sure it was ‘checked’ and still nothing. What am I doing wrong? This add-in will save me a lot of time. - Also, what happens when I send my spreadsheet to another user that does not have your add-in, does the concat still work?
Chandoo
July 14th, 2008 at 2:43 pm
10@Colebro: Hey.. that is strange, I remember Amit having similar problem. Did you try copying the code and saving it in to your own excel file in a new module? If you do that, then even if the excel file is sent to another person, the function still works… but if you save it as an add-in then the other computer should also have the addin installed.
Let me know if copying the code helps you… otherwise I will investigate in to this further….
Chandoo
July 14th, 2008 at 3:44 pm
11@colebro: Looks like this is not an error with the how you add the add-in but with the add-in it self…
My udf would work fine as long as the input range has strings (text) in them, but when the range has numbers in them the udf would throw #value error. Here is a fix…
replace this part of the code with:
For Each cell In useThis
retVal = retVal + cell.Value + dlm
Next
For Each cell In useThis
retVal = retVal + cstr(cell.Value) + dlm
Next
essentially I am force converting each cell’s value to string before creating the concatenated value… this seems to work when you have numbers / dates etc in the cells. Let me know if this helps you…
colebro
August 9th, 2008 at 2:05 pm
12I must be doing something real stupid because I can not get this to work. I removed the Add-in, changed the statement in the code above and re-added the xla to the add-in. I put a value (either numeric or alpha) and I get an invalid name (#NAME?)
a b c d e
=concat(a1:a6)
#NAME?
SheilaC
August 18th, 2008 at 9:22 pm
13BEAUTIFUL - I can’t say anything more. THANK YOU SO MUCH! Now, if the function could just skip blank cells…in other words, if in concat(b1:b8,”; “) there were 3 blank cells, it wouldn’t return something like this
text 1; text 2; text 3; ; ; text 6; ; text 8
but instead would return only
text 1; text 2; text 3; text 6; text 8
I would fall over and get rugburn on my nose. Even just what you wrote is a huge help - joining large cells, and getting real sick of having to join subsets, then aggregate the subsets in another “overall” concat statement.
LOL - might be too practical for us, right???
Chandoo
August 19th, 2008 at 2:05 pm
14@colebro: Oops, I missed to respond to this comment, let me see if I can fix this for you…
Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
Dim retVal, dlm As String
retVal = ""
If delim = Null Then
dlm = ""
Else
dlm = delim
End If
For Each cell In useThis
retVal = retVal + cstr(cell.Value) + dlm
Next
If dlm "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function
this should work… if not try saving the function and your spreadsheet… often excel takes sometime to figure out that the udf is now present. let me know if this doesnt help…
Chandoo
August 19th, 2008 at 2:25 pm
15@SheilaC: Welcome to PHD, thanks for the awesome comments… I am happy you found this really useful…
I have added an if condition in the loop to only add the cell if the contents are not blank… hope this helps you in getting that rugburn
Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
Dim retVal, dlm As String
retVal = ""
If delim = Null Then
dlm = ""
Else
dlm = delim
End If
For Each cell In useThis
if cstr(cell.value)<>“” and cstr(cell.value)<>” ” then
retVal = retVal + cstr(cell.Value) + dlm
end if
Next
If dlm “” Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function
let me know if this doesnt help…
Chandoo
August 19th, 2008 at 2:36 pm
16@SheilaC: you may want to replace the dirty quotes in the code with proper double quotes…
SheilaC
August 20th, 2008 at 11:44 pm
17…Sheila would like to reply to Chandoo - unfortunately she is in the hospital with grade 1 rugburn…in fact, they are extracting rug from her nostrils as we speak.
OMG Chandoo - if you were here - I WOULD HUG YOU SO BAD YOU’D SQUEEK!
Major help like you have no idea.
Sheila
MikeP
August 26th, 2008 at 5:01 pm
18Other quick edit
change line:
retVal + cstr(cell.Value) + dlm
to:
retVal & cstr(cell.Value) & dlm
Otherwise Excel sends an error when cell.value is a number
Chandoo
August 26th, 2008 at 5:45 pm
19@SheilaC : Hope you are alright :P, I am happy this helped you.
@MikeP : thanks for pointing it out, I have changed the code to include your suggestion.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Welcome :)
Quick Facts
I am Chandoo, when I started this blog in 2004, the purpose was to post snapshots from hell to share my b-school life with world.
Today this blog has over 600 articles in topics like Excel, Business, Advertising, Technology, Photography and Life in Indian B-schools.
Interested? Know more or mail me at: chandoo [dot] d [at] gmail [dot] com
Recent Comments
Categories
Recently PHD wrote
Monthly Archives
Category-wise Archives
Blogroll
PHD Link Love
Hungry for Spam
Disclaimer
Add PHD to your news reader
Tag Cloud
advertising b-school blogging business chennai company cost engineering Excel Tips experience food Friends fun game Humor idea ideas IIM images India Indore information learning management marketing MBA microsoft money movie office photos presentation product project service story technology tips train travel tricks tv visualization web weekendPointy Haired Dilbert - Chandoo.org is proudly powered by WordPress - BloggingPro theme by: Design Disease