Extract text from 2 strings on excel - excel

I need help to do this.
I have on:
A1 cell "Portugal + Spain + UK + Belgium"
B1 cell " France + Germany + Spain + italy"
And i want on D1 the common word on a1 and b2. in this example "spain"
is this possible?
tks

I would use "Text to Columns" like described here. Next I would use the formula =FILTER(list1,COUNTIF(list2,list1))
So consider that list1 is in cells A1:D1 and list2 is in cells A2:D2
the next cell (what ever cell) would have the formula:
=FILTER(A1:D1,COUNTIF(A2:D2,A1:D1))

I've looked at documentation and think the following would be supported within Excel 2021:
=LET(x,FILTERXML("<t><s>"&SUBSTITUTE(A1,"+","</s><s>")&"</s></t>","//s"),TEXTJOIN(" + ",,FILTER(x,ISNUMBER(FIND("+ "&x&" +","+ "&B1&" +")),"")))
Or, if you would be interested in a xpath based solution:
=TEXTJOIN(" + ",,FILTERXML("<x><t>"&TEXTJOIN("</s></t><t>",,FILTERXML("<t><s>"&SUBSTITUTE(A1,"+","</s><s>")&"</s></t>","//s")&"<s>+ "&B1)&" +</s></t></x>","//t[.//*[contains(.,concat('+ ',../text(),' +'))]]"))

I have tried & I got the below ans..
in Cell D3
=FILTER(TEXTSPLIT(TEXTJOIN(" + ",,A3:B3)," + "),TEXTSPLIT(TEXTJOIN(" + ",,A3:B3)," + ")=C3)
with the Filter function the result will be array.
and the basic result which supports all versions of excel is
in Cell D3
=IFERROR(MID($A3&" + "&$B3,SEARCH("#",SUBSTITUTE($A3&" + "&$B3,$C$3,"#",COLUMNS($D3:D3))),LEN($C$3)),"-")
have to drag the formula right side until get the result "-".

Related

Looking for a way to split cells in excel based the last five numbers in a cell

I am looking for a way to fix a mixed data set representing addresses in Excel.
Here is an example dataset.
Kirchgasse 2389179 Beimerstetten
All this should result in three datasets.
A1 = Kirchgasse 2389179 Beimerstetten
A2 = Kirchgasse 23
A3 = 89179
A4 = Beimerstetten
Pattern is, that A3 is always the last 5 numbers in the dataset.
I am totally lost here, as Excel is usually not my weapon of choice.
You can use Excel Formulas as well, to split cells, Excel gives us ample opportunity to use the features, there are lot of ways for a query to resolve, you can follow any of the following approaches, to accomplish the desired output
To Extract The First Part From Cell A1, Formula Used In Cell A2
=LEFT($A1,FIND(" ",$A1)+2)
To Extract The Second Part From Cell A1, Formula Used In Cell A3
=LEFT(REPLACE($A1,1,LEN($A2),""),5)
To Extract The Last Part Of The String From Cell A1, Formula Used In Cell A4
=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))
However To Extract The 5 Numbers, you can use any one of the following as well that suits with your Excel Version
Formula shown in cell A11
=MAX(IFERROR(MID($A$1,ROW(INDIRECT("1:"&LEN($A$1))),5)*1,""))
Formula shown in cell A13
=LARGE(IFERROR(--MID($A1,SEQUENCE(LEN($A1)),COLUMN($A$1:$E$1)),0),1)
Formula shown in cell A15
=AGGREGATE(14,6,IFERROR(--MID($A1,ROW($1:$10000),COLUMN($A$1:$E$1)),0),1)
Formula shown in cell A17
=--RIGHT(FILTERXML("<p><w>"&SUBSTITUTE($A1," ","</w><w>")&"</w></p>","//w[.*0=0]"),5)
To get a detailed knowledge on FILTERXML Function you can refer the link below, prepared by JvdV Sir
FILTERXML FUNCTION DETAILED ANALYSIS BY JvdV Sir
Here is few more ways
Formula used in cell A2
=SUBSTITUTE(TRIM(FILTERXML("<p><w>"&SUBSTITUTE(SUBSTITUTE(LEFT($A1,FIND(" ",$A1)+2)&REPLACE($A1,1,FIND(" ",$A1)+2," ")," ","#",1)," ","</w><w>")&"</w></p>","//w")),"#"," ")
Just enter the formula in cell A2, and it will spill if you are using O365 or Excel 2021
And if you are not using the above Excel Version then you may try this and Fill Down The Formula
Formula Used In Cell B2 & Fill Down
=SUBSTITUTE(TRIM(FILTERXML("<p><w>"&SUBSTITUTE(SUBSTITUTE(LEFT($A$1,FIND(" ",$A$1)+2)&REPLACE($A$1,1,FIND(" ",$A$1)+2," ")," ","#",1)," ","</w><w>")&"</w></p>","//w["&ROW(A1)&"]")),"#"," ")
In A2, formula copied down :
=TRIM(SUBSTITUTE(MID(SUBSTITUTE(" "&REPLACE(REPLACE(A$1,AGGREGATE(14,6,FIND(ROW($1:$10)-1,A$1,COLUMN(A:Z)),1)-4,0," "),FIND(" ",A$1),1,"#")," ",REPT(" ",50)),ROW($A1)*50,50),"#"," "))
Put Kirchgasse 2389179 Beimerstetten in the A1 cell, and use the below VBA codes to set the D1 cell value as the last 5 numbers
Sub Test_Pattern()
Dim stringOne As String
Dim regexOne As Object
Set regexOne = New RegExp
regexOne.Pattern = "(\d{5})(?!.*\d)"
regexOne.Global = TRUE
regexOne.IgnoreCase = IgnoreCase
stringOne = Range("A1").Value
Set theMatches = regexOne.Execute(stringOne)
For Each Match In theMatches
Range("D1").Value = Match.Value
Next
End Sub
To learn how to use regex in VBA, see this ref

Expanding a number string formula

I have an Excel formula that searches a string of 4 numbers, in a single cell comma separated, then divides them by 2 (rounding down).
e.g. cell A1 = 5,35,44,7
Formula in cell B1 = 2,17,22,3
I would like to extended the formula to accommodate a string of numbers anywhere from 1 through 15. The current formula that works perfect for a string of 1 to 4 numbers is below. I'm wanting to keep the formula to a single cell without using VBA. Thoughts on how to expand what I currently have?
=LEFT(IFERROR(ROUNDDOWN(MID(A2,1,IFERROR(SEARCH(",",A2,1)-1,LEN(A2)))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,1)+1)-SEARCH(",",A2,1)-1,LEN(A2)-SEARCH(",",A2,1)))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)-SEARCH(",",A2,SEARCH(",",A2,1)+1)-1,LEN(A2)-SEARCH(",",A2,SEARCH(",",A2,1))))/2,0),"")&","&IFERROR(ROUNDDOWN(MID(A2,SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)+1,IFERROR(SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)+1)-SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)-1,LEN(A2)-SEARCH(",",A2,SEARCH(",",A2,SEARCH(",",A2,1)+1)+1)))/2,0),""),LEN(A2))
This approach can handle nearly an unlimited number of items in a single cell, but it requires more than one formula.
Place the value in A1. In A2 enter:
=IFERROR(--ROUNDDOWN(TRIM(MID(SUBSTITUTE(A$1,",",REPT(" ",999)),ROWS($1:1)*999-998,999))/2,0),"")
and copy down. (these are the parsed individual values, divided by two, and then rounded down)
In B2 enter: =A2. In B3 enter:
=B2 & IF(A3="","","," & A3)
and copy down. (this performs the re-concatenation). Finally in C1 enter:
=INDEX(B:B,MATCH("",A:A,0))
It is MUCH easier to use VBA to solve this problem. This small UDF() does the same thing:
Public Function Brad(inpt As String) As String
Dim a
ary = Split(inpt, ",")
With Application.WorksheetFunction
For Each a In ary
Brad = Brad & "," & .RoundDown(CDbl(a) / 2, 0)
Next a
Brad = Mid(Brad, 2)
End With
End Function

return only values in a row

I have a list of unique ids and values on one sheet in excel as in the image below:
Is there a way to return only the values in the order they appear (left to right) on a separate sheet/location? For example, I would want to return for ID '1002' the values 35,32,44.. not the blanks. I am then going to turn those values into a Sparkline.
I am using excel 2010, if that makes a difference.
Thanks!
Sam
Try this formula:
=SUBSTITUTE(TRIM(B2 & " " & C2 & " " & D2 & " " & E2 & " " & F2)," ",",")
EDIT:
_______________________________________________________________________________
Hope this is not too late to answer your question.
Lets assume your data is in Sheet1 as in the image below:
Now, in Cell B2 of Sheet2 enter the following formula:
=IFERROR(INDEX(Sheet1!$B2:Sheet1!$F2, SMALL(IF(ISBLANK(Sheet1!$B2:Sheet1!$F2), "", COLUMN(Sheet1!$B2:Sheet1!$F2)-MIN(COLUMN(Sheet1!$B2:Sheet1!$F2))+1), COLUMN(A1))),"")
This is an array formula so commit it by pressing Ctrl+Shift+Enter
Drag this formula across till Column F and down till Row 4 or as required. This will give you following result.
I guess this is what you are looking for.
Based on "Concatenating Names with Delimiters" by Allen Wyatt you can use the formula below to achieve the result you wanted.
=MID(IF(B2<>"",","&B2,"")&IF(C2<>"",","& C2,"")&IF(D2<>"",","&D2,"")&IF(E2<>"",","&E2,"")&IF(F2<>"",","&F2,""),2,2000)
Regards,

How to concatenate strings from hundreds of cells?

I am trying to combine sentences from various cells into one cell. Example:
A1 - "hello"
A2 - "how are"
A3 - "you"
A4 - =combine(A1:A3) ---- > "hello how are you"
I know this trick: =A1 & " " & A2..... but I have 700 cells to combine into a single cell and this approach seems barbaric. If there is a built in function, that would be ideal. I don't mind an answer in VBA as long as the answer is extremely detailed starting with how to open VBA, as I do not know VBA.
There is but it isn't much better: concatenate.
In your case, that would be concatenate(A1;" ";A2;" ";A3).
Neither are good ways to deal with 700 cells.
A VBA solution would be better. To open the VBA editor, press ALT+F11 (for a graphical explanation, see this article).
Afterwards, go to the "Insert" menu on top and select "Module". The editor will then be ready to accept input. Just paste the following (delete any other text that it may have):
Option Explicit
Public Function MyConcatenate(ByVal myRange As Range)
Dim vCell, vResult As String
For Each vCell In myRange.Cells
vResult = vResult & " " & vCell.Text
Next
MyConcatenate = Mid(vResult, 2)
End Function
You can now close the editor and return to the workbook.
To use it, write the following in a cell: =MyConcatenate(A1:A3).
You can use the StringConcat function found in this page: http://www.cpearson.com/excel/stringconcatenation.aspx
Then you can use it like this:
=StringConcat("|",B1:B5)
An you will received all values from the range: B1:B5 separated by |.
In case the link is broken, you can find the source of the function here: http://pastebin.com/JNS9pYWA.
For a relatively modest 700 cells formulae would seem viable.
in B1: =A1&" "
in C1: =B1
in B2: copy B1 (ie =A2&" ")
in C2: =C1&B2
then copy down B2:C2 as required.
I too have the same problem. I have data in column A from A1:A980.
But I have found out the solution as below.
In B2 I put the formula as =CONCATENATE(B1," ",A1," ",A2)
and from B3 I entered formula as =CONCATENATE(B2," ",A3) and in B3 as =CONCATENATE(B3," ",A4) till B980.
This will give you your result in last B90 cell and that too without any VBA.
Hope you might have the same problem then this might solve the issue.

Append same text to every cell in a column in Excel

How can I append text to every cell in a column in Excel? I need to add a comma (",") to the end.
Example:
email#address.com turns into email#address.com,
Data Sample:
m2engineers#yahoo.co.in
satishmm_2sptc#yahoo.co.in
threed_precisions#rediffmail.com
workplace_solution#yahoo.co.in
threebworkplace#dataone.in
dtechbng#yahoo.co.in
innovations#yahoo.co.in
sagar#mmm.com
bpsiva#mmm.com
nsrinivasrao#mmm.com
pdilip#mmm.com
vvijaykrishnan#mmm.com
mrdevaraj#mmm.com
b3minvestorhelpdesk#mmm.com
sbshridhar#mmm.com
balaji#mmm.com
schakravarthi#mmm.com
srahul1#mmm.com
khramesh2#mmm.com
avinayak#mmm.com
rockindia#hotmail.com
See if this works for you.
All your data is in column A (beginning at row 1).
In column B, row 1, enter =A1&","
This will make cell B1 equal A1 with a comma appended.
Now select cell B1 and drag from the bottom right of cell down through all your rows (this copies the formula and uses the corresponding column A value.)
Select the newly appended data, copy it and paste it where you need using Paste -> By Value
That's It!
It's a simple "&" function.
=cell&"yourtexthere"
Example - your cell says Mickey, and you want Mickey Mouse. Mickey is in A2. In B2, type
=A2&" Mouse"
Then, copy and "paste special" for values.
B2 now reads "Mickey Mouse"
It's simple...
=CONCATENATE(A1, ",")
Example: if email#address.com is in the A1 cell then write in another cell: =CONCATENATE(A1, ",")
email#address.com After this formula you will get email#address.com,
For remove formula: copy that cell and use Alt + E + S + V or paste special value.
There is no need to use extra columns or VBA if you only want to add the character for display purposes.
As this post suggests, all you need to do is:
Select the cell(s) you would like to apply the formatting to
Click on the Home tab
Click on Number
Select Custom
In the Type text box, enter your desired formatting by placing the number zero inside whatever characters you want.
Example of such text for formatting:
If you want the cell holding value 120.00 to read $120K, type $0K
Pretty simple...you could put all of them in a cell using the concatenate function:
=CONCATENATE(A1, ", ", A2, ", ", and so on)
Highlight the column and then Ctrl + F.
Find and replace
Find ".com"
Replace ".com, "
And then one for .in
Find and replace
Find ".in"
Replace ".in, "
Simplest of them all is to use the "Flash Fill" option under the "Data" tab.
Keep the original input column on the left (say column A) and just add a blank column on the right of it (say column B, this new column will be treated as output).
Just fill in a couple of cells of Column B with actual expected output. In this case:
m2engineers#yahoo.co.in,
satishmm_2sptc#yahoo.co.in,
Then select the column range where you want the output along with the first couple of cells you filled manually ... then do the magic...click on "Flash Fill".
It basically understands the output pattern corresponding to the input and fills the empty cells.
I just wrote this for another answer:
You would call it using the form using your example: appendTextToRange "[theRange]", ",".
Sub testit()
appendTextToRange "A1:D4000", "hey there"
End Sub
Sub appendTextToRange(rngAddress As String, append As String)
Dim arr() As Variant, c As Variant
arr = Range(rngAddress).Formula
For x = LBound(arr, 1) To UBound(arr, 1)
For y = LBound(arr, 2) To UBound(arr, 2)
Debug.Print arr(x, y)
If arr(x, y) = "" Then
arr(x, y) = append
ElseIf Left(arr(x, y), 1) = "=" Then
arr(x, y) = arr(x, y) & " & "" " & append & """"
Else
arr(x, y) = arr(x, y) & " " & append
End If
Next
Next
Range(rngAddress).Formula = arr
End Sub
Select the range of cells, type in the value and press Ctrl + Enter.
This, of course, is true if you want to do it manually.
Put the text/value in the first cell, then copy the cell, mark the whole colum and 'paste' the copied text/value.
This works in Excel 97 - sorry no other version available on my side...
This is addition to #Edward-Leno 's answer for more detail/explanation and cases where the text cells are formulas instead of values, and you want to retain the original formula.
Suppose your cells look like this (formulas)
="email" & "#" & "address.com"
=A1 & "#" & C1
instead of this (values)
email#address.com
If "email" and "address.com" were some cells like A1 is the email and C1 is the address.com part, then you'd have something like =A1&"#"&C1 which would be important to retain since A1 and C1 might not be constants and can change, so the comma-concatenated values would change, like if C1 is "gmail.com", "yahoo.com", or something else based on its formula.
Values method: The following steps will successfully append text but only keep the value using a scratch column (this works for rows, too, but for simplicity, the directions are for columns)
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=A1&","
Essentially, the "&" is the concatenation operator, combining two strings together (numbers are converted to strings). The "," can be adjusted to ", " if you want a space after the comma.
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Formula retention method: The following steps will successfully retain the original formula. The process is similar to the values method, and only step 2, the formula used to concatenate the comma, changes.
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=FORMULATEXT(A1)&","
FORMULATEXT() grabs the formula of the cell as opposed to the value of it, so a simple example would be that it grabs =2+2 instead of 4, or =A1 & "#" & C1 where A1 is "Bob" and C1 is "gmail.com" instead of Bob#gmail.com.
Note: This formula only works for Excel versions 2013 and greater. For alternative equivalent solutions for Excel 2010 and older, see this superuser answer: https://superuser.com/a/894441/495155
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Type it in one cell, copy that cell, select all the cells you want to fill, and paste.
Alternatively, type it in one cell, select the black square in the bottom-right of that cell, and drag down.

Resources