How to merge rows in a column into one cell in excel? - excel

E.g
A1:I
A2:am
A3:a
A4:boy
I want to merge them all to a single cell "Iamaboy"
This example shows 4 cells merge into 1 cell however I have many cells (more than 100), I can't type them one by one using A1 & A2 & A3 & A4 what can I do?

If you prefer to do this without VBA, you can try the following:
Have your data in cells A1:A999 (or such)
Set cell B1 to "=A1"
Set cell B2 to "=B1&A2"
Copy cell B2 all the way down to B999 (e.g. by copying B2, selecting cells B3:B99 and pasting)
Cell B999 will now contain the concatenated text string you are looking for.

I present to you my ConcatenateRange VBA function (thanks Jean for the naming advice!) . It will take a range of cells (any dimension, any direction, etc.) and merge them together into a single string. As an optional third parameter, you can add a seperator (like a space, or commas sererated).
In this case, you'd write this to use it:
=ConcatenateRange(A1:A4)
Function ConcatenateRange(ByVal cell_range As range, _
Optional ByVal separator As String) As String
Dim newString As String
Dim cell As Variant
For Each cell in cell_range
If Len(cell) <> 0 Then
newString = newString & (separator & cell)
End if
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(separator)))
End If
ConcatenateRange = newString
End Function

Inside CONCATENATE you can use TRANSPOSE if you expand it (F9) then remove the surrounding {}brackets like this recommends
=CONCATENATE(TRANSPOSE(B2:B19))
Becomes
=CONCATENATE("Oh ","combining ", "a " ...)
You may need to add your own separator on the end, say create a column C and transpose that column.
=B1&" "
=B2&" "
=B3&" "

In simple cases you can use next method which doesn`t require you to create a function or to copy code to several cells:
In any cell write next code
=Transpose(A1:A9)
Where A1:A9 are cells you would like to merge.
Without leaving the cell press F9
After that, the cell will contain the string:
={A1,A2,A3,A4,A5,A6,A7,A8,A9}
Source: http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/
Update: One part can be ambiguous. Without leaving the cell means having your cell in editor mode. Alternatevly you can press F9 while are in cell editor panel (normaly it can be found above the spreadsheet)

Use VBA's already existing Join function. VBA functions aren't exposed in Excel, so I wrap Join in a user-defined function that exposes its functionality. The simplest form is:
Function JoinXL(arr As Variant, Optional delimiter As String = " ")
'arr must be a one-dimensional array.
JoinXL = Join(arr, delimiter)
End Function
Example usage:
=JoinXL(TRANSPOSE(A1:A4)," ")
entered as an array formula (using Ctrl-Shift-Enter).
Now, JoinXL accepts only one-dimensional arrays as input. In Excel, ranges return two-dimensional arrays. In the above example, TRANSPOSE converts the 4×1 two-dimensional array into a 4-element one-dimensional array (this is the documented behaviour of TRANSPOSE when it is fed with a single-column two-dimensional array).
For a horizontal range, you would have to do a double TRANSPOSE:
=JoinXL(TRANSPOSE(TRANSPOSE(A1:D1)))
The inner TRANSPOSE converts the 1×4 two-dimensional array into a 4×1 two-dimensional array, which the outer TRANSPOSE then converts into the expected 4-element one-dimensional array.
This usage of TRANSPOSE is a well-known way of converting 2D arrays into 1D arrays in Excel, but it looks terrible. A more elegant solution would be to hide this away in the JoinXL VBA function.

For those who have Excel 2016 (and I suppose next versions), there is now directly the CONCAT function, which will replace the CONCATENATE function.
So the correct way to do it in Excel 2016 is :
=CONCAT(A1:A4)
which will produce :
Iamaboy
For users of olders versions of Excel, the other answers are relevant.

For Excel 2011 on Mac it's different. I did it as a three step process.
Create a column of values in column A.
In column B, to the right of the first cell, create a rule that uses the concatenate function on the column value and ",". For example, assuming A1 is the first row, the formula for B1 is =B1. For the next row to row N, the formula is =Concatenate(",",A2). You end up with:
QA
,Sekuli
,Testing
,Applitools
,Visual Testing
,Test Automation
,Selenium
In column C create a formula that concatenates all previous values. Because it is additive you will get all at the end. The formula for cell C1 is =B1. For all other rows to N, the formula is =Concatenate(C1,B2). And you get:
QA,Sekuli
QA,Sekuli,Testing
QA,Sekuli,Testing,Applitools
QA,Sekuli,Testing,Applitools,Visual Testing
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation,Selenium
The last cell of the list will be what you want. This is compatible with Excel on Windows or Mac.

I use the CONCATENATE method to take the values of a column and wrap quotes around them with columns in between in order to quickly populate the WHERE IN () clause of a SQL statement.
I always just type =CONCATENATE("'",B2,"'",",") and then select that and drag it down, which creates =CONCATENATE("'",B3,"'",","), =CONCATENATE("'",B4,"'",","), etc. then highlight that whole column, copy paste to a plain text editor and paste back if needed, thus stripping the row separation. It works, but again, just as a one time deal, this is not a good solution for someone who needs this all the time.

I know this is really a really old question, but I was trying to do the same thing and I stumbled upon a new formula in excel called "TEXTJOIN".
For the question, the following formula solves the problem
=TEXTJOIN("",TRUE,(a1:a4))
The signature of "TEXTJOIN" is explained as TEXTJOIN(delimiter,ignore_empty,text1,[text2],[text3],...)

I needed a general purpose Concatenate With Separator (since I don't have TEXTJOIN) so I wrote this:
Public Function ConcatWS(separator As String, ParamArray cell_range()) As String
'---concatenate with seperator
For n = LBound(cell_range) To UBound(cell_range)
For Each cell In cell_range(n)
If Len(cell) <> 0 Then
ConcatWS = ConcatWS & IIf(ConcatWS <> "", separator, "") & cell
End If
Next
Next n
End Function
Which allows us to go crazy with flexibility in including cell ranges:
=ConcatWS(" ", Fields, E1:G2, L6:M9, O6)
NOTE: "Fields" is a Named Range and the separator may be blank

Related

Split into columns using three different separators

I want to convert an Excel field into three columns based on these three delimiters: forward slash, dash, and close parenthesis: ) / -
And here is what I need to see:
I've tried the 'Text to Columns' Data Tool in excel, but it cannot accept three custom separators at once.
I want a simple and fast Excel method that can convert the following pattern:
You can try FILTERXML/SUBSTITUTE:
=SUBSTITUTE(FILTERXML("<a><b>'" & SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A2,"/","</b><b>'"),")","</b><b>'"),"-","</b><b>'") & " </b></a>","//b[" & COLUMN(A:A) & "]"),"'","")
One approach is to first fix the data and then run TextToColumns. Select the cells you wish to process and run this short VBA macro:
Option Explicit
Sub FixPhoneData()
Dim cell As Range, v As String
For Each cell In Selection
With cell
v = .Value
v = Replace(v, ")", "-")
v = Replace(v, "/", "-")
.Value = v
End With
Next cell
End Sub
Before:
and after:
If one has Excel O365, you could try:
Formula in B1:
=TRANSPOSE(MID(CONCAT(IFERROR(MID(A1,SEQUENCE(LEN(A1)),1)*1,"")),{1,4,7},{3,3,4}))
Now you can throw any pattern at the formula as long as you provide the usual 10 digits.
If always these three same patterns, you could also use:
=TRANSPOSE(MID(RIGHT(A1,12),{1,5,9},{3,3,4}))
This last, simpler solution can also be used in Excel prior to O365 if one used INDEX():
=INDEX(MID(RIGHT($A1,12),{1,4,7},{3,3,4}),COLUMN(A1))
Drag this formula right, and down.
If your goal was to ultimately have this pattern in a single cell then things became much easier in an instant, and you can use:
=REPLACE(RIGHT(A1,12),4,1,"-")
If you need to do this in GS then try:
=SPLIT(REPLACE(RIGHT(A1,12),4,1,"-"),"-")

using INDIRECT function with dynamic array formula syntax (Excel O365)

I am using the new dynamic array functions introduced in excel in 2018 (e. g. SEQUENCE, UNIQUE etc. functions).
I have a list of cell references that are that are generated dynamically, and would like to apply the INDIRECT function to these list items. A simplified example:
cell A1: =SEQUENCE(5) (results in rows column A values 1,2,3,4,5 as expected)
cell B1: ="A"&A1# (results in rows column B values A1, A2, A3, A4, A5 as expected)
cell C1: =INDIRECT(B1#) this should give me rows in column C values 1,2,3,4,5, but in fact gives me #VALUE ,#VALUE ,#VALUE ,#VALUE ,#VALUE
So the formula properly recognizes the number of rows of the original dynamic array, but for some reason does not dereference the cells properly. The strings seem to be of the proper format - a simple string function such as LEN also works: setting C1 to =LEN(B1#) results in 5 rows of the value 2.
The syntax per se seems to be OK.. for the special case of =SEQUENCE(1) in cell A1 everything works as intended. I tried the R1C1 reference format also, same result
EDIT
Overall I am trying to achieve the following
import a list form a non-Excel data source list is not a dynamic array, it's just a TSV import. I don't now beforehand how many items are in this list, and it can vary a lot
do several different calculations on values of this list.
so far my approach was to use the COUNT function to determine the number of items in the imported list, and then use that to create the second list using SEQUENCE and INDEX to retrieve values.
the problem arises for some calculations where the data contains references to other rows so I have to use indirect addressing to get at that data
The INDIRECT function cannot accept an array for an argument.
In other words:
=INDIRECT({"a1","a2"}) --> #VALUE! | #VALUE!
So you could, for example, refer to each cell in column B as a single cell:
eg:
C1: =INDIRECT(B1)
and fill down.
Depending on how you are using this, you could also use the INDEX function to return an individual element
To return the third element in the array generated by B1#:
=INDIRECT(INDEX(B1#,3))
EDIT:
After reading your comment, and depending on details you have not shared, you may be able to use a variation of the INDEX function.
For example, to return the contents of A1:A5, based on your SEQUENCE function, you can use:
=INDEX($A:$A, SEQUENCE(5))
but exactly how to apply this to your actual situation depends on the details.
As Rosenfeld points out, INDIRECT() does not accept an array as an input. If you need a function that:
"acts" like INDIRECT()
can accept an array as an input
can return an array as an output
Then we can make our own:
Public Function Indirect_a(rng As Range)
Dim arr, i As Long, j As Long
Dim rngc As Long, rngr As Long
rngc = rng.Columns.Count
rngr = rng.Rows.Count
ReDim arr(1 To rngr, 1 To rngc)
For i = 1 To rngc
For j = 1 To rngr
arr(j, i) = Range(rng(j, i).Value)
Next j
Next i
Indirect_a = arr
End Function
and use it like:
Since it creates a "column-compatible" array, it will spill-down dynamically in Excel 365.It can be used in versions of Excel prior to 365, but it must be array-entered into the block it occupies.
You can use the following formula
=BYROW(B1#,LAMBDA(a,INDIRECT(a)))

concatenate multiple matches in excel

Please see below
I want to concatenate 'comments' in table 2 into table 1 as shown in the series of images without using TEXTJOIN() or macros. Only using regular excel functions
There is no simple solution without using UDF or helper columns. I would suggest using UDF formula which is simple to implement and use in worksheets. To use this approach, please enter this code in your regular module(module1).
Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)
Dim i As Long
Dim result As String
For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & " " & Return_val_col.Cells(i, 1).Value
End If
Next
Lookup_concat = Trim(result)
End Function
now you can use this UDF just like regular worksheet formula. Enter this formula =Lookup_concat(G3,$D$3:$D$12,$E$3:$E$12) in cell I3 and drag it to the bottom.
in case you want to use only regular formulas, you will need to enter this formula =IFERROR(INDEX($D$3:$E$12, SMALL(IF(($G3=$D$3:$D$12), ROW($D$3:$D$12)-MIN(ROW($D$3:$D$12))+1, ""),COLUMNS($A$1:A1)), 2),"") in cell K3 using CTRL+SHIFT+ENTER combination since it is an array formula. Now drag formmula to the right and down(Estimate how far to the righ your formula needs to go in order to catch all unique values).
Then enter this formula =CONCATENATE(K3," ",L3," ",M3," ",N3," ",O3," ") in cell J3 and drag it to the bottom (adjust formula to estimated number of unique values).
There's a simple way to do this. :) Please see this Google sheet for a working example.
You can use the FILTER and JOIN functions to achieve this:
=iferror(join(", ", filter(E$3:E$12, D$3:D$12 = G3)))
In the above example the FILTER function will look at cells D3:D12 and try to find rows matching the value in G3. For the matching rows, the FILTER function returns the values from cells E3:E12 as an array.
JOIN is used to join the array items together with a comma in between.
Finally, IFERROR gets rid of N/A errors resulting from FILTER not matching anything.
(Kudos to original answer here https://stackoverflow.com/a/23367059/36817)
You will need to add a helper column to achieve your goal.
Assuming you have the helper column C and this is the array formula (means you have to click Ctrl + Shift + Enter altogether) you should try:
{=IF(OR(ROW(C1)=1,MAX(--($A$1:A1=A2)*ROW($A$1:A1))=0),B2,INDEX($C$1:C1,MAX(--($A$1:A1=A2)*ROW($A$1:A1)))&", "&B2)}
Now at column G assuming this is the place you want to get your outcome, you can enter this array formula (means you have to click Ctrl + Shift + Enter altogether):
{=IFERROR(INDEX($A$2:$C$11,MAX(--($A$2:$A$11=E2)*ROW($A$2:$A$11))-1,3),"")}
This way you should get the results you are expecting.

Check id every value in ";" separated string exists on a LOV list using just Excel formulas

In a cell I have a multi value separated by semicolon like this:
Red;Blue;Green
I need to compare if each of those values exist on a list:
Black
Orange
Green
Blue
Red
I think it should be an array formula, but I have no idea how to set it.
Is it even possible?
Regards
Michał
You've not mentioned what output you are looking for. Below are the two possible solution.
1. If you are looking for the count of words in a cell from the list use following formula:
=SUMPRODUCT(ISNUMBER(FIND($E$2:$E$6,$A2))*1)
2. If you want words in the cell that are in the list to be displayed in separate columns, use the following array formula
=IFERROR(INDEX($J$2:$J$6,SMALL(IF(ISNUMBER(FIND($J$2:$J$6,$A2)),ROW($J$2:$J$6)-ROW($J$1)),COLUMNS($A1:A1))),"")
Drag/copy above formula across and down as required.
Being an array formula you'll have to commit this formula by pressing Ctrl+Shift+Enter.
You can write this UDF and use it as a formula. Wasn't sure what output is required. This UDF gives number of items that match in the list.
Parameters:
myValue - the cell that contains multi value separated by semicolon
listRange - Range that has the list to check against. Should be a single column list
Function checkList(myValue As Range, listRange As Range) As Integer
Dim t As Variant
t = Split(myValue.Value, ";")
Dim c As Integer
c = 0
For i = LBound(t) To UBound(t)
For j = 1 To listRange.Rows.Count
If (t(i) = listRange(j, 1)) Then
c = c + 1
End If
Next j
Next i
checkList = c
End Function
Since you want to do this only with excel formulas, the input string has to be split to multiple cells before comparing it with the list.
If your input string is in A1, use the below formula and drag it right to split them based on the delimiter ;.
=TRIM(MID(SUBSTITUTE($A1,";",REPT(" ",999)),1+((COLUMN(A1)-1)*999),999))
Assuming your list is in column G, use the below formula which counts the strings Red, Blue and Green in your list and returns Found or Not found.
in C2,
=IF(COUNTIF($G:$G,C1),"Found","Not found")
Hope this helps.

how to separate text with multiple underscores and varying length of values

a1=ac_tree_birch_NewYork_ext
a2=bc_animal_dog_Washington_des
How do I separate the text in the cells by the "_", since there is varying length of the cell values. I would like to use a formula, and not text to columns.
Thanks
Use the SUBSTITUTE function to change all underscores (e.g. CHAR(95)) to a large number of spaces (typically the entire length of the original string) and peel out the padded pieces with the MID function. Finish off with TRIM and an IFERROR 'wrapper'.
In B1 as,
=IFERROR(TRIM(MID(SUBSTITUTE($A1, CHAR(95), REPT(CHAR(32), LEN($A1))), (COLUMN(A:A)-1)*LEN($A1)+1, LEN($A1))), TEXT(,))
Fill both right and down.
  
This can likely be done via Flash Fill (Excel 2013+).
For the first row of data, enter your expected outcome in subsequent cells to the right. This is how you want the data broken up:
Then select your first cell of output data and click Flash Fill from the ribbon:
Do this for the remaining columns. This will fill the column based on the pattern recognized by Excel within your original data:
If a VBA solution is acceptable, you can write a wrapper around the VBA Split function:
Public Function Split2(s As String) As String()
Split2 = Split(s, "_")
End Function
Then in your worksheet, select (say) cells B1:F1, enter
=Split2(A1)
as an array function (CTRL-SHIFT-ENTER), and out comes your data.
Hope that helps.

Resources