Extract Last 2 line breaks from excel - excel

I'm trying to achieve the below:
I would like cell AD3 to pull in the last 2 lines of text from cell AC3, which will be variable and often changing. The text in cell AC3 is all separated by line breaks.
In case you're wondering, currently I just have the values typed into cell AD3 for demonstration of my goal.
Thank you!

Let's break this down. For ease of writing, I use A1.
You first want to know how many rows are in the cell.
=LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))
This formula returns a count of the character 10, which is the character used for a line break in a cell. If the cell has 5 rows, there will be 4 line break characters. If you want to return the last 2 rows, you want everything after the one but last line break character. To identify the one but last line break, subtract 1.
=LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))-1
Replace the one but last line break character with a character that you know will otherwise not be in your cell, for example the character with code 160, which is a non-printable blank.
=SUBSTITUTE(A1,CHAR(10),CHAR(160),LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))-1)
Next, you want to find the position of the character 160
=FIND(CHAR(160),SUBSTITUTE(A1,CHAR(10),CHAR(160),LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))-1))
Now that you know the position of that character, you can use MID() to return the text after that character (add 1 to the position of that character). Assume that the last two rows of text in A1 are never more than 99 characters, use that for how many characters you want to return. Or use your favourite big number that will do that.
=MID(A1,FIND(CHAR(160),SUBSTITUTE(A1,CHAR(10),CHAR(160),LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))-1))+1,99)
Remember to format the cell with the formula to wrap!

Many ways to do this. Here's one that is easily adaptable to returning other lines.
If you have the functions available in your version of Excel, you can use FILTERXML and TEXTJOIN
=TEXTJOIN(CHAR(10),,FILTERXML("<t><s>"&SUBSTITUTE(A1,CHAR(10),"</s><s>")&"</s></t>","//s[position()>last()-2]"))
"<t><s>"&SUBSTITUTE(A1,CHAR(10),"</s><s>")&"</s></t>" creates an XML where the line breaks divide the nodes
the xpath argument "//s[position()>last()-2]") returns the last two nodes. Obviously this can be modified an many ways to return other nodes
TEXTJOIN then joins those nodes with a linebreak.

Return the last 2 lines
In B1, enter formula :
=TRIM(RIGHT(SUBSTITUTE(A1,CHAR(10),REPT(" ",399)),799))

Related

reformat excel text column to specific format

I have a column in my excel that includes authors name and it looks as follows:
My goal is to remove the dates + the last comma from all of these rows to make it something like this:
Is there a way I can do it in excel?
Based on your example, in which there are multiple commas in one cell, I would go with determining the position of the last comma first (in order to know where to slice the content of said cell). Then it's a matter of IF formula based on condition in which the last 4 characters in the cell are digits:
=IF(ISNUMBER(VALUE(RIGHT(A1,4))),LEFT(A1,FIND("#",SUBSTITUTE(A1,",","#",LEN(A1)-LEN(SUBSTITUTE(A1,",",""))))-1),A1)
FYI: The "#" substitution is targeted at knowing exactly where the last comma occurs in the cell. Any other unique, not-appearing-in-the-string character would have done the same job.
I've tested the formula on below examples:

Error #VALUE! when calculate with result of hlookup

This first formula, i used HLOOKUP to fill data for column J OK
=HLOOKUP(LEFT(C6,1),$E$18:$H$19,2,0)
A second formula, i would like to fill data for column K but error #VALUE!
=((G6/26)*H6*J6)+((G6/26)*I6)
Description image
What am I doing wrong?
Thanks in advance!
Excel's overhead can take care of text-that-look-like-a-number to true number conversion 'on the fly'. All you have to do is stick the text in a maths operation and the conversion is automatic.
="1"+1 'result is 2
=1+1 'result is 2
However, if there are extra characters attached to the text-that-look-like-a-number, Excel cannot perform the conversion and throws a #VALUE! error.
="1"&char(10)+1 'result is #VALUE!
The easiest way to check for non-printing¹ (invisible) characters like line feeds or CHAR(0) atoms is to check the length with the LEN(...) function. If the length is greater than what we can see there is likely non-printing characters attached.
For your data,
Not only is the 17 left-aligned (the default for Text), the LEN function shows that it is 3 characters wide, not the 2 characters wide that we can see.
Go back to the original value where HLOOKUP retrieved its lookup and select the cell and tap F2. Using the arraow keys will show where the rogue character is.
Delete the rogue character and your formula should resolve properly.
¹ Typical non-printing characters are line feeds (Chr(10)) and carriage returns (Chr(13), non-breaking spaces (Chr(160), atom delimiters like Chr(0) or zero-width spaces (Crw(8203). Text-that-look-like-a-number that has rogue characters attached is very common from data imported of copied from web sources.

How to search for X character set item in cell divided by underscore

Let's say I have the following value in a cell "test1_test2_test3_test4_test5". In another cell it could be "test1_test2_test3" or even "test 1_t est2".
What I would like is to have a 'general' function that I can specify to only give me back e.g. all characters before the first underscore, between the first en second underscore etc...and all the characters after the last underscore. And....if there isn't anything found, don't give back an error but just empty or nothing.
Thusfar I've googled a working format for when having a maximum of 2 underscores present (each different in formula):
For locating and displaying the characters before the first underscore: =LEFT(D32; SEARCH("";D32;1)-1)
For locating the characters after the first and before the second underscore: =MID(D32;SEARCH("";D32;1)+1;SEARCH("";D32;SEARCH("";D32;1)+1)-(SEARCH("";D32;1))-1)
For locating the characters after the second underscore (not limiting untill the next one is/is not present): =RIGHT(D32;LEN(D32)-SEARCH("";D32;SEARCH("_";D32;1)+1))
Ps: because my native (excel) language is Dutch, I've done my best to translate my working Excel functions to the English syntax.
With data in A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,"_",REPT(" ",999)),COLUMNS($A:A)*999-998,999))
and copy across:
I suggest Text to Columns with underscore as the delimiter, count how many pieces result (COUNTA) and then pick to suit accordingly. Use IF to return blank ("") if say you want the text after the second underscore and the count is 1.

How to split using the last backslash character as delimiter in excel 2013?

We have a list with 1000's of values in a single column.The values will be in this format
W:\RT_QAQC\Received\20160411_GDM_QA\VD\RegRef\Afr\Geology\IAE_Geology_Africa_10M
W:\RT_QAQC\Received\20160411_GDM_QA\VD\GlobRef\Ind\GIS\SS_GIS_Ind
I would like to split it using the last backslash a delemiter. So the result will be
W:\RT_QAQC\Received\20160411_GDM_QA\VD\RegRef\Afr\Geology
W:\RT_QAQC\Received\20160411_GDM_QA\VD\GlobRef\Ind\GIS
And in the next column,
IAE_Geology_Africa_10M
SS_GIS_Ind
I've tried with this one
=TRIM(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",LEN(A1))),LEN(A1)))
But it's only copying the characters after the last backslash
How about:
=MID(A1,1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))-1)
This uses a simple technique to determine the position of the last occurrence of a substring in a string.
Another approach is to use tigeravatar's approach
Assuming your source data is in column A and you do not have a header row and your data start in row 1. In C1 use the formula you posted as it seems to do what you want for the second column. It also makes our life easIer on how to deal with the first column. In B1 place the following.
=LEFT(A1,LEN(A1)-LEN(C1)-1)
Results

Finding words that start with a specific sign

I have a few thousands rows in an Excel file with a line of text in each cell. In this line of text, there is sometimes a word that starts with the character "&". I would like to avoid using VBA.
If the words that start with "&" were always the same length, I would use "LEFT" or "RIGHT". What Excel function would you advise me to use to extract these words?
Other question: If I have two words that start with "&" in the same cell, is there any way to have two different functions, in two other cells, one looking for the first one starting from the beginning, the other one looking for the last one starting from the end?
Thanks.
Regarding your first question. Say in A1 there is your first string. Put in B1 this formula:
=IF(LEFT(SUBSTITUTE(A1," &"," "),1)="&",MID(SUBSTITUTE(A1," &"," "),2,10000),SUBSTITUTE(A1," &"," "))
Then drag down (copy formulas down) for cells A2, A3 ecc..
This take care of all the words that are preceded by a space and the first word in the cell. You have to look out of special case (interpunctions ecc..) as: "bla bla,&Word"
Left and Right are still a great functions to use.
Say the word under inspection is K8.
You may obtain the first character with =IF(LEFT(K8,1)="&",TRUE,FALSE).
You may obtain all the characters but the first by using =RIGHT(K8,LEN(K8)-1).
Of course, you may replace the TRUE in the first statement with the RIGHT... of the second statement; I have broken them out for clarity.
try the INSTR function to find the first occuance of &
instr(string,"&") 'returns the 1st occurance of &
then, if you need to find another occurance
instr(n,string,"&") 'returns the 1st occurance starting in position n - which can be 1+ the result of the prior line
and INSTRREV(string, "&") will find the last occurance

Resources