need to merge 3 columns in excel - excel

I have 3 columns A,B,C and I need to merge the 3 columns and I have applied the forumala =A1&","&B1&","&C1 the output came as E column I need the output as D column.

The following formula will achieve your desired result:
=TEXTJOIN(",",TRUE,A1:C1)
Textjoin works like concatenate but can have a delimiter as an argument, also it gives you the ability to ignore blank cells, the first argument is the delimiter, the second is the flag to ignore blanks and the third is for the range.
As comments do mention that TEXTJOIN is only available for Office 365 subscribers, a possible alternative would be to build your UDF as below, this will allow you to use the formula above without an Office 365 subscription:
Function TEXTJOIN(delimiter As String, ignore_empty As Boolean, rng As Range) As String
Dim compiled As String
For Each cell In rng
If ignore_empty And IsEmpty(cell.Value) Then
'nothing
Else
compiled = compiled + IIf(compiled = "", "", delimiter) + CStr(cell.Value)
End If
Next
TEXTJOIN = compiled
End Function

Enter this formula into E1:
=CONCATENATE(A1, IF(AND(B1<>"", A1<>""), ",", ""), B1,
IF(AND(OR(A1<>"", B1<>""), C1<>""), ",", ""), C1)
Using TEXTJOIN might be a cleaner option, but is only available on more recent versions of Excel.

'Put this code in module and use formula concmulti and select the range
Function concmulti(slt As Range) As String
Dim str As String
Dim cell As Range
For Each cell In slt
str = str & cell.Value & ", "
Next cell
concmulti = str
End Function

If there are no spaces within the cells, then in D1 enter:
=SUBSTITUTE(TRIM(A1 & " " & B1 & " " & C1)," ",",")
and copy downwards:

Related

How to append to text based on condition

How to create a formula in result column based on the values of the previous column
if values in any previous columns (from c1 to c5) is greater than zero, I want to add it to the result.
If you happen to have access to TEXTJOIN function you can use the following:
Formula in F2:
=TEXTJOIN(",",TRUE,IF(A2:E2>0,$A$1:$E$1,""))
Note 1: It's an array formula and needs to be confirmed through CtrlShiftEnter
Note 2: Without access to TEXTJOIN this will become a much more painfull process, but for just the 5 columns you can try =IF(A2>0,A$1&",","")&IF(B2>0,B$1&",","")&IF(C2>0,C$1&",","")&IF(D2>0,D$1&",","")&IF(E2>0,E$1&",",""), however you'll have to expand to get rid of the trailing comma
If both the above options are not feasible, your best bet will be to write a UDF. For this exercise, you can write a rather simple one:
Public Function TextJoin2(rng1 As Range, rng2 As Range) As String
With Application
Dim arr As Variant: arr = .Evaluate("IF(" & rng1.Address & ">0," & rng2.Address & ","" "")")
TextJoin2 = Replace(.Trim(Join(arr, " ")), " ", ",")
End With
End Function
Call in F2 like: =TEXTJOIN2(A2:E2,A$1:E$1) and drag down.
You can use =CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")) as array formula, confirm with ShiftCtrlEnter.
Put this formula in F2 cell in your example. Expand that formula to whole range
What remains here is to remove comma on the end of result string.
That full formula would be like this
=LEFT(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";""));LEN(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")))-1)

Joining a range of cells in excel using &

I know I can concatenate a set of cells using & like this:
A1 & B1 & C1 & D1. Is there a way to write this for a range A1:P1 using & ?
You can accomplish this with a formula in a series of columns (each sequentially concatenating from the previous) or with a UDF (User Defined Function).
Formula Method
       
The formula in F2 is,
=A2
The formula in G2 is,
=IF(LEN(B2), F2&","&B2, F2)
Fill G2 right to catch all of the available text values.
VBA UDF Method
Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the following into the new pane titled something like Book1 - Module1 (Code).
Function udf_stitch_Together(r As Range, Optional d As String = ", ") As String
Dim s As String, c As Range
For Each c In r
If CBool(Len(c.Text)) Then _
s = s & IIf(Len(s), d, vbNullString) & c.Text
Next c
udf_stitch_Together = s
End Function
Tap Alt+Q to return to your worksheet.
       
The syntax is simply,
udf_stitch_Together(<range to concatenate>, <[optional] delimiter>)
For your purposes this would be,
=udf_stitch_Together(A2:P2)
  ... or,
=udf_stitch_Together(A2:P2, ", ")
Fill down as necessary. Note that I have used =udf_stitch_Together(A5:P5, ";") in F5:F6 to demonstrate the ability to change the delimiter.
For a 1D range as you have (a single column or row), you could try this
For a Row, your example, it would be called like
=StrCat(A1:P1, TRUE)
Function StrCat(rng1 As Range, bRows As Boolean) As String
If bRows Then
'row
StrCat = Join(Application.Transpose(Application.Transpose(rng1)), ", ")
Else
'column
StrCat = Join(Application.Transpose(rng1), ", ")
End If
End Function
I know this is an old question but I found a way to use Concatenate like #brettdj was talking about. Using the method from this link you can join together a range using a combination of Concatenate and Transpose like so:
Let say the cells you want to combine are in B2:B19.
In a blank cell, where you want to concatenate all the values type =CONCATENATE(TRANSPOSE(B2:B19))
Don’t press enter yet.
Select the TRANSPOSE(B2:B19) portion and press F9. This replaces the TRANSPOSE(B2:B19) with its result
Now remove curly brackets { and }
Press Enter & you're Done!
Demo from link:
(source: chandoo.org)

Concatenate different columns values in a cell

Let's supose 'large' excel like this:
A |B
ab |ef
|oo
ut |
|oo
ut |ef
That I need is a new row with a summary of all differents values of each column:
A |B
ab |ef
|oo
ut |
|oo
ut |ef
ab,ut |ef,oo <- new row with the 'summary'
Note: I can copy by hand the formula at end of each column, I only need the formula
Following is a function that can be used to Concatenate unique columns values in a cell
Function UniqueItem(InputRange As Range) As Variant
Dim cl As Range, cUnique As New Collection, cValue As Variant
Application.Volatile
On Error Resume Next
For Each cl In InputRange
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
End If
Next cl
UniqueItem = ""
For i = 1 To cUnique.Count
If UniqueItem = "" Then
UniqueItem = UniqueItem & cUnique(i)
ElseIf UniqueItem <> "" Then
UniqueItem = UniqueItem & ", " & cUnique(i)
End If
Next
On Error GoTo 0
End Function
How to use this function
1. Open excel file
2. Press Alt + F11
3. Create a new module and paste the code in it
4. Go back to the excel file and select the cell you want to have the result
5. Enter formula as =UniqueItem(A1:A5) A1:A5 specifies the range. You can specify any range.
Please find the sample file at the following link: Concatenate_different_columns_values_in_a_cell.xlsm
With some tricks (and an extra column) you can do this the following way.
In column A row 1 through 6, I placed some random text.
In column B I placed the following formulae.
B1: =IF(COUNTIF($A$1:$A1,A1)=1,A1&", ","")
B2: =IF(COUNTIF($A$1:$A2,A2)=1,A2&", ","")
B3: =IF(COUNTIF($A$1:$A3,A3)=1,A3&", ","")
B4: =IF(COUNTIF($A$1:$A4,A4)=1,A4&", ","")
B5: =IF(COUNTIF($A$1:$A5,A5)=1,A5&", ","")
B6: =IF(COUNTIF($A$1:$A6,A6)=1,A6&", ","")
B7: =SUBSTITUTE(B1&B2&B3&B4&B5&B6&",",", ,","")
The idea here is that the search list grows from row 1 to the end and marks any unique value. If the statement in B1 through B6 is true, the value from A is used and a , {space} is added.
In B7 I just concatenate all values, then I add an extra comma at the end.
The substitute removes the last ,{space}, with nothing, effectively making sure the list does not end with a comma.
You can paste the formula for B1 in cell B1 and then just paste the formula down. The relative reference automatically increases the search array.
Understand. One (not so elegant) way is the following... Set C1: = B1, the C2: =B1&B2, then copy C2 all the way down to the end.... On the other hand, I think this might be better served by a VBA solution.
Alternatively, you could use this...
http://mcgimpsey.com/excel/udfs/multicat.html
Regards,
Robert Ilbrink

Add text of cell range in one cell in Excel

I want to concatenate a range of cells into one cell, without macros/vbscript.
The formula CONCATENATE() gets individual cells.
Its not that easy, but I end up with a solution that works wonders!
A1: the text to search
B1:BN: The range within the results would go
B5: The delimiter text
=MID($A$1,LEN(CONCAT($B$1:B1))+COUNTA($B$1:B1)*LEN($B$5)+1,
SEARCH(
$B$5,
$A$1,
LEN(
CONCAT($B$1:B1)) + COUNTA($B$1:B1)*LEN($B$5)+1)
-(LEN(CONCAT($B$1:B1))+COUNTA($B$1:B1)*LEN($B$5)+1))
As for now it works perfect. Note that you can use whatever text as delimiter. In my case it was "comma + space".
Where Concat is a simple function that concatenates a range of cells:
Function Concat(myRange As Range) As String
Dim r As Range
Application.Volatile
For Each r In myRange
If Len(r.Text) Then
Concat = Concat & IIf(Concat <> "", "", "") & r.Text
End If
Next
End Function
You can either use CONCATENATE() or & to join cells. There is no range you can grab all at once unless you use a UDF such as MCONCAT.

Is there a programmatic interface to Excel's Array Formulae?

For example, suppose I have
{={1,2,3,4,5}}
in a cell. Is there any way to find out how many elements are in that array, and what those elements are?
Now implementing #chris neilsen's idea, I've now got a VBA function as below
Function rinfo(r As Range) As String
With r.CurrentArray
rinfo = .Address & ", " & .Rows.Count & ", " & .Columns.Count & ", " & .Value & ", " & .Value2
End With
End Function
however the data from it doesn't look to hopeful, viz
$A$29, 1, 1, 1, 1
The Rows.Count and Columns.Count make sense if they are counting the rows and cols used in the worksheet. But as an indication of the data in the array formula, no.
Your formula only occupies a single cell, and a cell can only contain a scalar value, so after the cell has been calculated it will contain 1. But you can evaluate the formula from VBA and that can give you an array result. If your formula is in A1 then
Dim vArr As Variant
vArr = Evaluate(Range("a1").Formula)
will result in vArr containing an array of of 4 variants of the 4 numbers. There are several "quirks" to the Evaluate method: see one of my web pages for details.
In a Sub, if cl is a Range and is set to a cell that is part of an array formula, then
cl.CurrentArray
returns the range that contains the array formula.
If your example formula was in cells A1:E1 and the activecell is any of cells A1 .. E1, then running
Sub zx()
MsgBox "ref = " & ActiveCell.CurrentArray.Address
End Sub
would return a message of ref = $A$1:$E$1
You can use .Rows.Count and .Columns.Count to get the size, and .Value to get the values

Resources