How to extract middle characters from a cell in Excel - excel

I need a excel function code that would enable to me extract certain characters in the middle of a cell.
So in cell A74 is:
1625362674848-cdpresent-auths_ol_mart-auths1837372
So I Need to extract "auths_ol_mart" into a separate column
I have tried this:
=MID(A2, SEARCH("-",A2) + 1, SEARCH("-",A2,SEARCH("-",A2)+1) - SEARCH("-",A2) - 1)
Now the problem is this only gets "cdpresent". I am not quite sure how this is done.
more examples include:
3837463747-cdpresent-avaya_op_history-clm1827489
I want "avaya_op_history"
3734279458-cdpresent-uk_score_app-clm9377233
I want "uk_score_app"
Thank you all

You can use this worksheet formula:
=LEFT(MID(A1,FIND("-",A1,FIND("-",A1)+1)+1,99),FIND("-",MID(A1,FIND("-",A1,FIND("-",A1)+1)+1,99))-1)
If you use the Formula Evaluation tool, you will be able to see how this works.
If you prefer a UDF, you can use:
Function betweenDashes(S As String) As String
betweenDashes = Split(S, "-")(2)
End Function
Edit (20MAR2020)
Another formula to return the third item in the string, if you have Excel 2013+ and the FILTERXML function:
=FILTERXML("<t><s>" & SUBSTITUTE(A1,"-","</s><s>")& "</s></t>","//s[3]")
or, if you prefer, the next to last item:
=FILTERXML("<t><s>" & SUBSTITUTE(A1,"-","</s><s>")& "</s></t>","//s[last()-1]")

An alternative way to formula is Text to Columns with hyphen (-) as a delimiter. You get what you are looking for in the 3rd column:

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,"-"),"-")

Extract partial string between two same characters with Excel

I have in cell A1 the following value
Kate/Nancy/Judy
If I want to retrieve Nancy I need to use
=SUBSTITUTE(MID(SUBSTITUTE("/" & A3&REPT(" ",6),"/",REPT(",",255)),2*255,255),",","")
What if I want to retrieve Judy or Kate? I cannot simply adjust MID to LEFT or RIGHT because these functions have different characteristics.
Thank you
=TRIM(MID(SUBSTITUTE(U20,"-",REPT(" ",100)),200,100))
You could try:
Select the data
Data tab - Data Tools - Text to Columns
Select Delimited - Next
Select Other and use "/" (without the double quotes) - Next - Finish
Results:
If you want to do this with one formula, you could use:
Formula in B2:
=TRIM(MID(SUBSTITUTE($A2,"/",REPT(" ",LEN($A2))),(B$1-1)*LEN($A2)+1,LEN($A2)))
Drag right and down.
More information about this formula can be found on this website.
We could also make use of VBA's SPLIT() function quite nicely, like so:
Function GetNth(Rng As String, Delmt As String, Nth As Long) As String
Dim Arr() As String
Arr() = Split(Rng, Delmt)
GetNth = Arr(Nth - 1)
End Function
Call in your worksheet like: =GetNth(A2,"/",1), =GetNth(A2,"/",2) or =GetNth(A2,"/",3) to return Kate, Nancy or Judy respectively.

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.

Count number of numbers in a single cell

I want to count the number of numbers in a single cell.
Example:
In cell A1, I have the following addition:
=12+45+23+51+10 which totals (and shows) 141.
In cell B1, I would like the see how many numbers have been added together, which means that there should be 5 (12 is a number, 45 another one, etc... and all together, there are 5 numbers in cell A1).
I know it seems to be a ridiculous question, but I scanned all the platforms for this issue and did not find any suitable solution. Tried all the LEN and LEN SUBSTITUTE alternatives out there, but somehow it does not work.
Thank you upfront for your help. Optimal solution would be a excel formula, alternatively VBA would also work.
Excel 2013 has a new function Formulatext() which returns the, well, formula text. Using that the Len() and Substitute() approach works.
=LEN(FORMULATEXT(A1))-LEN(SUBSTITUTE(FORMULATEXT(A1),"+",""))+1
Hit Alt+F11 and Insert->Module with the following VBA:
Function NumCount(Rng As Range)
x = Split(Rng.Formula, "+")
NumCount = UBound(x) + 1
End Function
Then you can call =NumCount(A1) on any cell to get the number of numbers in the formula for that cell.
Use this VBA:
Function GetFormula(Cell as Range) as String
GetFormula = Cell.Formula
End Function
and then to get the number of numbers...
=LEN(GetFormula(D99))-LEN(SUBSTITUTE(GetFormula(D99),"+",""))
on this as my D99 contents...
=45+46+47+50+100
The one major drawback here is that I'm assuming everything is + if you have -, *,/ or other operators this gets more challenging. you might be able to use replace for each but you'd always be limited to the 4 basic operators... if someone used exponents or mod, this would be harder.
Also possible in earlier Excel versions without VBA - subject to (i) there is always at least one value in the cells, (ii) the operator is always + and (iii) the cells are in a column.
Replace = with '= in that column, apply the substitution, say:
=LEN(A1)-LEN(SUBSTITUTE(A1,"+",""))+1
in a different column and Paste Special, Value the results for that other column. Then apply Text To Columns on the original column with ' as the delimiter.
*There is no way to do this without using a User Defined Function (UDF) written in Excel VBA. Something like this should work:
Public Function numsAdded(cell1 As Range)
Dim formulaString As String
formulaString = cell1.Formula
numsAdded = Len(formulaString) - Len(Replace(formulaString, "+", "")) + 1
End Function
Once you've added that to a VBA module, you can use it as a function in any cell in your spreadsheet.
*Edit - Apparently there is a way if you have Excel 2013, as teylyn suggests. If you use 2010 or earlier like me, you'll need VBA.
Try this:
=LEN(SUBSTITUTE(F16,"+","+"))
Note: F16 is only an example name for the cell you want to do the counting on.
Example:
F16=45+65+76 # Gives 186
F17=LEN(SUBSTITUTE(F16,"+","+")) # Gives 3

How to merge rows in a column into one cell in 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

Resources