Given a spreadsheet cell containing a string that consists of a hyphenated series of character segments, I need to extract the last segment.
For example, consider column A containing data strings like XX-XXX-X-XX-XX-G10, where X denotes any character. What formula would I need to place in column B to get G10 as a result?
A B
1 XX-XXX-X-XX-XX-G10 G10
I'm looking for a formula that could work in in Libre Office Calc, Open Office Calc, MS Excel, or Google Sheets.
Another possibility in LO Calc is to use the general purpose regular expression macro shown here: https://superuser.com/a/1072196/541756. Then the cell formula would be similar to JPV's answer:
=REFIND(A1,"([^-]+$)")
If you are using google sheets, regexextract would be possible too:
=REGEXEXTRACT(A1, "[^-]+$")
In LibreOffice Calc and OpenOffice Calc, you can use a regular expression to determine the position of the text after the last - character:
=SEARCH("-[:alnum:]+$";A1)
will return 15 if A1 contains XX-XXX-X-XX-XX-G10.
Now, you can use this value to get the text "behind" that position, using the RIGHT() function:
=RIGHT(A1;LEN(A1)-SEARCH("-[:alnum:]+$";A1))
Split up on multiple lines:
=RIGHT( ' return text beginning from the right...
A1; ' of cell A1 ...
LEN(A1) ' start at lenght(A1) = 18
- ' minus ...
SEARCH( ' position ...
"-[:alnum:]+$" ' of last "-" ...
;A1 ' in cell A1 = 15 ==> last three characters
)
)
It appears that you want the characters that appear at the end of a string, to the right of the last instance of a hyphen character, "-".
This formula, adapted from here, works in Excel, *Calc & Google Sheets:
=TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),LEN(A1)))
Explanation:
SUBSTITUTE(A1,"-",new_string) will find each hyphen ("-") in the original string from cell A1 and replace it with a new_string.
REPT(" ",LEN(A1)) is a string of repeated space characters (" "), the same length as the original string in cell A1.
TRIM(RIGHT(string,count)) will get the right-most count characters, and trim off leading and trailing spaces. Since the string was previously padded out by replacing hyphens with spaces, and count is the same LEN(A1) used for that padding, the last count characters consists of a bunch of spaces followed by whatever followed the last hyphen!
In Google Sheets, an alternative approach is to use the SPLIT function to break the value from column A into an array, then select the last element. (Excel-VBA has a split() function, so you could make this work in Excel by writing VBA code to provide it as a custom function.)
=INDEX(SPLIT(A1,"-"),0,COUNTA(SPLIT(A1,"-")))
I found simply solution:
=RIGHT(A1;3)
that gives me G10 as the result too! It works because COL A always have 3 chars at the end!
Related
I would like to use the =indirect() and =value() function together. I am to count how many numbers are > 0 in another sheet, in the rows I:I. In these sheets, the numbers are not formatted as values, and I am not to do this manually.
In addition, for the numbers, they are formatted as
" 123 "
With a space before the number.
I also need to remove the space in front the numbers in order for this to work.
In my current sheet, in cell J1, I have written "I:I". In the cell A3, I have the name of the sheet that I would like to count the numbers from.
My attempt so far:
{=COUNT.IF(INDIRECT("'"&A3&"'!"&$J$1);">0")
However, this just returns values 0. This is because there is a space infront of the numbers. How do I remove this space? I need to implement it in this function
I've edited the post a bit, as I noticed there was a space infront of the numbers
On a worksheet, if your value " 123 " is in cell A1 then you could use a formula like:
=VALUE(TRIM(A1))
...to TRIM the spaces, and convert the string to a numeric VALUE.
Or, in VBA, something like:
myValue = CInt(Trim(myString))
...to Trim the spaces, and Convert to CInt-eger.
Or, in many ad-hoc situations it's easiest to use "Text to Columns" to remove padded spaces (or any other character, really).
More ways to convert text to numbers here and here.
More ways to remove unneeded spaces here and here.
I have a list of strings where I want to split the numbers and alphabets part. For e.g. in cell A1 I have "FNN-12345 - Sample Text - 2016_AA1.1" (without the quotes ""). I want to split it to get just "Sample Text - 2016_AA1.1".
Appreciate any guidance on the formula.
Cheers.
This is the universal solution, no matter what the first alphanumeric string is:
=RIGHT(A1,LEN(A1)-FIND(" - ",A1)-2)
It finds the first occurence of the string " - " and keeps only the part after that string.
You can use the functions to manipulate strings of characters in Excel like Left, Right or Mid to get the desired result in combination with a Lenghth function.
As such for you result you could try :
=RIGHT(A1, LEN(A1) - LEN("FNN-12345 - "))
This formula would take the length of the entire cell and remove the FNN-12345 - part. Of course you can add a column which contain the desired elements to be removed.
If the text which you want to select begin always on the position 13 (as in your example), use the formula
=RIGHT(A1,LEN(A2)-12)
(supposing your original text is in the cell A1).
If you recognize the start of text by the pattern " - ", use the formula
=RIGHT(A3,LEN(A3) -FIND(" - ",A4)-LEN(" - ")+ 1)
I have a string such as K68272CAA6A1
And need to do that, formula will pass the first character (I mean string will be 68272CAA6A1 in mind) and formula will find the first text character. And cell value will be 7. Because first text character is "C" and it's the 7th character of my string (include "K" character).
And after that I'll split rest of them. But I'm confused about this issue.
If I understand you correctly, you are looking for the position of the 2nd letter in your string. That number is given by the following array-entered formula.
To enter an array formula, hold down ctrl+shift while hitting Enter. If you do this correctly, in the Formula Bar you will see braces {...} around the formula:
=MATCH(FALSE,ISNUMBER(MID(A1,ROW(INDIRECT("2:99")),1)/1),0)+1
The 99 just needs to be some number larger than the length of your longest string.
If I understood you correctly, a formula that implements this functionality (assuming cell A1 = K68272CAA6A1 and B1 = K) would be:
=FIND(RIGHT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(B1,A1)),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),1),RIGHT(A1,LEN(A1)-FIND(B1,A1)))-1
The long sequence of substitute is there to remove the numbers (I couldn't find a specific formula to remove them).
This gigantic formula for your example would simply give the answer 6.
To get the strings separated as you want all you need to do is =LEFT(A1,D1) supposing the long formula is on D1 and =RIGHT(A1,D1), which in your example would yield respectively K68272 and CAA6A1
A program that exports to Excel creates a file with an indented list in a single column like this:
Column A
First Text
Second Text
Third Text
Fourth Text
Fifth Text
How can I create a function in excel that counts the number of white spaces before the string of text?
So as to return: 1 for the first text row and 3 for the for the thirst row, etc in this example.
Preferably seeking a non-VBA solution.
TRIM doesn't help here because it removes double spaces also between words.
The main idea is to find the FIRST letter in the trimmed string and find its position in the original string:
=FIND(LEFT(TRIM(A1),1),A1)-1
You can try this function in Ms Excel itself:
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))
This would apply if the results are in a single cell. If it is for a whole row/column, just drag the formula accordingly.
Try below:
=FIND(" ",A1,1)-1
It calculates the position of the first found whitespace character in a cell and reduces it by 1 to reflect number of characters before that position.
As per http://www.mrexcel.com/forum/excel-questions/61485-counting-spaces.html, you may try:
=LEN(Cell)-LEN(SUBSTITUTE(Cell," ",""))
where Cell is your target cell (i.e. A1, B1, D3, etc.).
My example:
B8: =LEN(F8)-LEN(SUBSTITUTE(F8," ",""))
F8: [ this is a test ]
produces 4 in B8.
The above method will count spaces before the string if any were inserted, between individual words and after the string, if any were inserted. It won't count available space that does not have an actual white space character. So, if I inserted two spaces after test in the above example, the total count would be raised to 6.
As has been pointed out in the other answers, you can't really use TRIM or SUBSTITUTE as potential spaces in between words or at the end will give you the wrong result.
However, this formula will work:
=MATCH(TRUE,MID(A1,COLUMN($A$1:$J$1),1)<>" ",0)-1
You need to enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
In case you expect more than 10 spaces, replace the $J with a column letter further down in the alphabet!
Here's my solution. If the left 5 characters equals "_____" (5 blank spaces), then return 5, else look for 4 spaces, and so on.
=IF(LEFT(B1,5)=" ",5,IF(LEFT(B1,4)=" ",4,IF(LEFT(B1,3)=" ",3,IF(LEFT(B1,2)=" ",2,1))))
You almost got it with LEN + TRIM in answers before, you only need to combine both:
=LEN(Cell)-LEN(TRIM(Cell))
If it is Indented you could create a Personal Function like this:
Function IndentLevel(Cell As Range)
'This function returns the indentation of a cell content
Application.Volatile
'With "Application.Volatile" you can make sure, that the function will be
recalculated once the worksheet is recalculated
'for example, when you press F9 (Windows) or press enter in a cell
IndentLevel = Cell.IndentLevel
'Return the IndentLevel
End Function
This will work only if it is Indented, you can see this property in the Cell Format -> Alignment.
After This you could see the Indentation Level.
I want to delete first characters in a cell and show it in another cell.
I have A1 - 1900: (1873, 'asd#asd.com
I want to show B1 - asd#asd.com
You can use the RIGHT function to 'keep' a number of characters:
=RIGHT(A1, LEN(A1)-B1)
Will chop off B1 characters of the content of cell A1 (in this case, B1 should contain 14, but you can make it a calculation, too)
If you have always a quote before your email, just select you column, go to Data / convert and use "'" as separator
If the apostrophe precede every e-mail address then this formula can be used in column B.
=MID(A1,FIND("'",A1)+1,LEN(A1))
The find function returns the first occurrence of the apostrophe.
The Mid function extracts the text after the apostrophe.