Is there an efficient way to identify the last character/string match in a string using base functions? I.e. not the last character/string of the string, but the position of a character/string's last occurrence in a string. Search
and find
both work left-to-right so I can't think how to apply without lengthy recursive algorithm. And this solution now seems obsolete.
标签:
excel-formula
相关问题
- Excel formula in VBA code
- Excel named ranges to make incrementing headers an
- How can I use the WEEKNUM function in a statement
- SUMIF(S) with VLOOKUP
- Turn a value range in a cell into a comma separate
相关文章
- How do you figure out the MEDIAN of a column takin
- Excel expression to copy rows but remove blank row
- Excel formula in counting distance between a speci
- How can I write an excel formula to return a value
- How do I fuzzy match just adjacent cells?
- Google spreadsheet relative row numbering + skip e
- How to get the formula cell value(data) using apac
- How do I prevent Excel from automatically replicat
Considering a part of a Comment made by @SSilk my end goal has really been to get everything to the right of that last occurence an alternative approach with a very simple formula is to copy a column (say
A
) of strings and on the copy (say ColumnB) apply Find and Replace. For instance taking the example:Drive:\Folder\SubFolder\Filename.ext
This returns what remains (here
Filename.ext
) after the last instance of whatever character is chosen (here\
) which is sometimes the objective anyway and facilitates finding the position of the last such character with a short formula such as:I'm a little late to the party, but maybe this could help. The link in the question had a similar formula, but mine uses the IF() statement to get rid of errors.
If you're not afraid of Ctrl+Shift+Enter, you can do pretty well with an array formula.
String (in cell A1): "one.two.three.four"
Formula:
Result: 14
First,
returns an array of integers from 1 to 99:
{1,2,3,4,...,98,99}
.Next,
returns an array of 1-length strings found in the target string, then returns blank strings after the length of the target string is reached:
{"o","n","e",".",..."u","r","","",""...}
Next,
compares each item in the array to the string "." and returns either the index of the character in the string or FALSE:
{FALSE,FALSE,FALSE,4,FALSE,FALSE,FALSE,8,FALSE,FALSE,FALSE,FALSE,FALSE,14,FALSE,FALSE.....}
Last,
returns the maximum value of the array:
14
Advantages of this formula is that it is short, relatively easy to understand, and doesn't require any unique characters.
Disadvantages are the required use of Ctrl+Shift+Enter and the limitation on string length. This can be worked around with a variation shown below, but that variation uses the OFFSET() function which is a volatile (read: slow) function.
Not sure what the speed of this formula is vs. others.
Variations:
I think I get what you mean. Let's say for example you want the right-most \ in the following string (which is stored in cell A1):
Drive:\Folder\SubFolder\Filename.ext
To get the position of the last \, you would use this formula:
That tells us the right-most \ is at character 24. It does this by looking for "@" and substituting the very last "\" with an "@". It determines the last one by using
In this scenario, the substring is simply "\" which has a length of 1, so you could leave off the division at the end and just use:
Now we can use that to get the folder path:
Here's the folder path without the trailing \
And to get just the filename:
However, here is an alternate version of getting everything to the right of the last instance of a specific character. So using our same example, this would also return the file name:
tigeravatar and Jean-François Corbett suggested to use this formula to generate the string right of the last occurrence of the "\" character
If the character used as separator is space, " ", then the formula has to be changed to:
No need to mention, the "{" character can be replaced with any character that would not "normally" occur in the text to process.
Very late to the party, but A simple solution is using VBA to create a custom function.
Add the function to VBA in the WorkBook, Worksheet, or a VBA Module
Then the cell formula
in a cell and the string to be searched in cell B1 will populate the cell with the text trailing the last "/" from cell B1. No length limit, no obscure formulas. Only downside I can think is the need for a macro-enabled workbook.
Any user VBA Function can be called this way to return a value to a cell formula, including as a parameter to a builtin Excel function.
If you are going to use the function heavily you'll want to check for the case when the character is not in the string, then string is blank, etc.
You could use this function I created to find the last instance of a string within a string.
Sure the accepted Excel formula works, but it's much too difficult to read and use. At some point you have to break out into smaller chunks so it's maintainable. My function below is readable, but that's irrelevant because you call it in a formula using named parameters. This makes using it simple.
I use it like this: