Sunday, 1 September 2019

worksheet function - How to concatenate values in Excel?


I have a spreadsheet that looks something like this:



   A B C D E ...
1| 1 0 0 1 0
2| 0 1 0 0 1
3| 0 0 0 1 0
4| 1 0 0 0 0



I'd like to concatenate row 1 in column F so that it returns: 10010


But entering =concatenate(A1:E1) returns an error #VALUE! and using an array formula ({=concatenate(A1:E1)}) returns just 1.


This method gets me close, but it removes the formula so I can't drag down and concatenate all rows.


Why does Excel make concatenating so hard? Is there another way to do this without using VBA?



Answer



In F1 enter:


=A1 & B1 & C1 & D1 & E1

and copy down.


enter image description here


EDIT#1:


If you want to bite the bullet and use VBA, then consider this tiny UDF


Public Function KonKatenate(rIN As Range) As String
Dim r As Range
For Each r In rIN
KonKatenate = KonKatenate & r.Text
Next r
End Function

User Defined Functions (UDFs) are very easy to install and use:



  1. ALT-F11 brings up the VBE window

  2. ALT-I ALT-M opens a fresh module

  3. paste the stuff in and close the VBE window


If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx


To remove the UDF:



  1. bring up the VBE window as above

  2. clear the code out

  3. close the VBE window


To use the UDF from Excel:


=KonKatenate(A1:Z1)

To learn more about macros in general, see:


http://www.mvps.org/dmcritchie/excel/getstarted.htm


and


http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx


and for specifics on UDFs, see:


http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx


Macros must be enabled for this to work!


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...