Find First Letter in a String
I need a formula to find and return the first letter in a string. So if a cell contains:
1234(*&^%$23ADFG54
the formula would return:
A
EDIT#1:
I am currently using the following UDF:
Public Function FirstLetter(Sin As String) As String
Dim i As Long, CH As String
FirstLetter = ""
For i = 1 To Len(Sin)
If Mid(Sin, i, 1) Like "[A-Z]" Then
FirstLetter = Mid(Sin, i, 1)
Exit Function
End If
Next i
End Function
But I need a non-VBA solution.
microsoft-excel
add a comment |
I need a formula to find and return the first letter in a string. So if a cell contains:
1234(*&^%$23ADFG54
the formula would return:
A
EDIT#1:
I am currently using the following UDF:
Public Function FirstLetter(Sin As String) As String
Dim i As Long, CH As String
FirstLetter = ""
For i = 1 To Len(Sin)
If Mid(Sin, i, 1) Like "[A-Z]" Then
FirstLetter = Mid(Sin, i, 1)
Exit Function
End If
Next i
End Function
But I need a non-VBA solution.
microsoft-excel
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37
add a comment |
I need a formula to find and return the first letter in a string. So if a cell contains:
1234(*&^%$23ADFG54
the formula would return:
A
EDIT#1:
I am currently using the following UDF:
Public Function FirstLetter(Sin As String) As String
Dim i As Long, CH As String
FirstLetter = ""
For i = 1 To Len(Sin)
If Mid(Sin, i, 1) Like "[A-Z]" Then
FirstLetter = Mid(Sin, i, 1)
Exit Function
End If
Next i
End Function
But I need a non-VBA solution.
microsoft-excel
I need a formula to find and return the first letter in a string. So if a cell contains:
1234(*&^%$23ADFG54
the formula would return:
A
EDIT#1:
I am currently using the following UDF:
Public Function FirstLetter(Sin As String) As String
Dim i As Long, CH As String
FirstLetter = ""
For i = 1 To Len(Sin)
If Mid(Sin, i, 1) Like "[A-Z]" Then
FirstLetter = Mid(Sin, i, 1)
Exit Function
End If
Next i
End Function
But I need a non-VBA solution.
microsoft-excel
microsoft-excel
edited Nov 30 '14 at 19:37
Gary's Student
asked Nov 30 '14 at 19:10
Gary's StudentGary's Student
13.7k31730
13.7k31730
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37
add a comment |
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37
add a comment |
2 Answers
2
active
oldest
votes
This is probably not the most elegant but it stays away from Ctrl+Shift+Enter.
=MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))<65)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>90))*1E+99,,))
Just Enter normally. Fill down as necessary. Note that the UPPER
makes it case-insensitive.
How It Works
- An A is ASCII character 65 and a Z is 90. These numbers can be derived from a single string character using the CODE function.
- You need to crawl through the string to be examined character by character. The MID function can peel out a single character from within the string but it needs a starting point which will increase by 1 until each character in the string has been examined.
- Typically this is done with the ROW function like
ROW(1:50)
to increment through numbers 1 to 50 if we knew that the string was 50 characters long. There is no guarantee of that string length so we have to construct the row reference using 1 and the LEN function then turn that concatenated string into a usable worksheet cell range address with the INDIRECT function. - The UPPER function was used on the string so that we only have to look for upper case ASCII character codes. If it wasn't in place the criteria would have to be doubled up to also compare to ASCII codes 97 (e.g. a) and 122 (e.g. z).
- The INDEX function is used in its array form to provide the iteration to crawl through the string character by character. Any character peeled out that is not within A-Z causes the number returned by
ROW(1:<length-of-string>)
at that position to be multiplied by 1E+99 (a very large number and not the minimum of anything on the worksheet). If it is within A-Z, the position orROW(1:<length-of-string>)
remains the same value. - So
INDEX
is returning an array of numbers (aka positions), some of which are in the 1E+99 range. The MIN function takes the smallest of these which represents the first alphabetic character in the string. - With that formula in the first cell, fill down as necessary to evaluate all the strings in column A. Easy-peasy.
It may be worthwhile to point out that using ROW
like this to provide an incremental series of number for array processing is usually down by locking down the actual numbers as absolutes like ROW($1:$50)
so they do not change when filled down. This is unnecessary here as the "1:" is text and will not change when the copied/filled to another location.
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
add a comment |
Another way to determine the position
Column B
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
This returns the position of the first letter found. Returns Len(A1) + 1 in case no letter is found.
The first argument of Find is an array of all letters to find in the string. The second argument is the actual string in upper case along with all letters at the end so that the find function always returns a value for all the letters.
Column C
=IF(LEN(A1) >= B1, RIGHT(LEFT(A1, B1),1), "")
This returns the letter found in the string. In case the string has no letter, it would return empty.
Reference:
https://exceljet.net/formula/split-text-and-numbers
The link provides a solution for numbers. The above formula is switched for letters.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f846822%2ffind-first-letter-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is probably not the most elegant but it stays away from Ctrl+Shift+Enter.
=MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))<65)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>90))*1E+99,,))
Just Enter normally. Fill down as necessary. Note that the UPPER
makes it case-insensitive.
How It Works
- An A is ASCII character 65 and a Z is 90. These numbers can be derived from a single string character using the CODE function.
- You need to crawl through the string to be examined character by character. The MID function can peel out a single character from within the string but it needs a starting point which will increase by 1 until each character in the string has been examined.
- Typically this is done with the ROW function like
ROW(1:50)
to increment through numbers 1 to 50 if we knew that the string was 50 characters long. There is no guarantee of that string length so we have to construct the row reference using 1 and the LEN function then turn that concatenated string into a usable worksheet cell range address with the INDIRECT function. - The UPPER function was used on the string so that we only have to look for upper case ASCII character codes. If it wasn't in place the criteria would have to be doubled up to also compare to ASCII codes 97 (e.g. a) and 122 (e.g. z).
- The INDEX function is used in its array form to provide the iteration to crawl through the string character by character. Any character peeled out that is not within A-Z causes the number returned by
ROW(1:<length-of-string>)
at that position to be multiplied by 1E+99 (a very large number and not the minimum of anything on the worksheet). If it is within A-Z, the position orROW(1:<length-of-string>)
remains the same value. - So
INDEX
is returning an array of numbers (aka positions), some of which are in the 1E+99 range. The MIN function takes the smallest of these which represents the first alphabetic character in the string. - With that formula in the first cell, fill down as necessary to evaluate all the strings in column A. Easy-peasy.
It may be worthwhile to point out that using ROW
like this to provide an incremental series of number for array processing is usually down by locking down the actual numbers as absolutes like ROW($1:$50)
so they do not change when filled down. This is unnecessary here as the "1:" is text and will not change when the copied/filled to another location.
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
add a comment |
This is probably not the most elegant but it stays away from Ctrl+Shift+Enter.
=MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))<65)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>90))*1E+99,,))
Just Enter normally. Fill down as necessary. Note that the UPPER
makes it case-insensitive.
How It Works
- An A is ASCII character 65 and a Z is 90. These numbers can be derived from a single string character using the CODE function.
- You need to crawl through the string to be examined character by character. The MID function can peel out a single character from within the string but it needs a starting point which will increase by 1 until each character in the string has been examined.
- Typically this is done with the ROW function like
ROW(1:50)
to increment through numbers 1 to 50 if we knew that the string was 50 characters long. There is no guarantee of that string length so we have to construct the row reference using 1 and the LEN function then turn that concatenated string into a usable worksheet cell range address with the INDIRECT function. - The UPPER function was used on the string so that we only have to look for upper case ASCII character codes. If it wasn't in place the criteria would have to be doubled up to also compare to ASCII codes 97 (e.g. a) and 122 (e.g. z).
- The INDEX function is used in its array form to provide the iteration to crawl through the string character by character. Any character peeled out that is not within A-Z causes the number returned by
ROW(1:<length-of-string>)
at that position to be multiplied by 1E+99 (a very large number and not the minimum of anything on the worksheet). If it is within A-Z, the position orROW(1:<length-of-string>)
remains the same value. - So
INDEX
is returning an array of numbers (aka positions), some of which are in the 1E+99 range. The MIN function takes the smallest of these which represents the first alphabetic character in the string. - With that formula in the first cell, fill down as necessary to evaluate all the strings in column A. Easy-peasy.
It may be worthwhile to point out that using ROW
like this to provide an incremental series of number for array processing is usually down by locking down the actual numbers as absolutes like ROW($1:$50)
so they do not change when filled down. This is unnecessary here as the "1:" is text and will not change when the copied/filled to another location.
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
add a comment |
This is probably not the most elegant but it stays away from Ctrl+Shift+Enter.
=MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))<65)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>90))*1E+99,,))
Just Enter normally. Fill down as necessary. Note that the UPPER
makes it case-insensitive.
How It Works
- An A is ASCII character 65 and a Z is 90. These numbers can be derived from a single string character using the CODE function.
- You need to crawl through the string to be examined character by character. The MID function can peel out a single character from within the string but it needs a starting point which will increase by 1 until each character in the string has been examined.
- Typically this is done with the ROW function like
ROW(1:50)
to increment through numbers 1 to 50 if we knew that the string was 50 characters long. There is no guarantee of that string length so we have to construct the row reference using 1 and the LEN function then turn that concatenated string into a usable worksheet cell range address with the INDIRECT function. - The UPPER function was used on the string so that we only have to look for upper case ASCII character codes. If it wasn't in place the criteria would have to be doubled up to also compare to ASCII codes 97 (e.g. a) and 122 (e.g. z).
- The INDEX function is used in its array form to provide the iteration to crawl through the string character by character. Any character peeled out that is not within A-Z causes the number returned by
ROW(1:<length-of-string>)
at that position to be multiplied by 1E+99 (a very large number and not the minimum of anything on the worksheet). If it is within A-Z, the position orROW(1:<length-of-string>)
remains the same value. - So
INDEX
is returning an array of numbers (aka positions), some of which are in the 1E+99 range. The MIN function takes the smallest of these which represents the first alphabetic character in the string. - With that formula in the first cell, fill down as necessary to evaluate all the strings in column A. Easy-peasy.
It may be worthwhile to point out that using ROW
like this to provide an incremental series of number for array processing is usually down by locking down the actual numbers as absolutes like ROW($1:$50)
so they do not change when filled down. This is unnecessary here as the "1:" is text and will not change when the copied/filled to another location.
This is probably not the most elegant but it stays away from Ctrl+Shift+Enter.
=MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))<65)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>90))*1E+99,,))
Just Enter normally. Fill down as necessary. Note that the UPPER
makes it case-insensitive.
How It Works
- An A is ASCII character 65 and a Z is 90. These numbers can be derived from a single string character using the CODE function.
- You need to crawl through the string to be examined character by character. The MID function can peel out a single character from within the string but it needs a starting point which will increase by 1 until each character in the string has been examined.
- Typically this is done with the ROW function like
ROW(1:50)
to increment through numbers 1 to 50 if we knew that the string was 50 characters long. There is no guarantee of that string length so we have to construct the row reference using 1 and the LEN function then turn that concatenated string into a usable worksheet cell range address with the INDIRECT function. - The UPPER function was used on the string so that we only have to look for upper case ASCII character codes. If it wasn't in place the criteria would have to be doubled up to also compare to ASCII codes 97 (e.g. a) and 122 (e.g. z).
- The INDEX function is used in its array form to provide the iteration to crawl through the string character by character. Any character peeled out that is not within A-Z causes the number returned by
ROW(1:<length-of-string>)
at that position to be multiplied by 1E+99 (a very large number and not the minimum of anything on the worksheet). If it is within A-Z, the position orROW(1:<length-of-string>)
remains the same value. - So
INDEX
is returning an array of numbers (aka positions), some of which are in the 1E+99 range. The MIN function takes the smallest of these which represents the first alphabetic character in the string. - With that formula in the first cell, fill down as necessary to evaluate all the strings in column A. Easy-peasy.
It may be worthwhile to point out that using ROW
like this to provide an incremental series of number for array processing is usually down by locking down the actual numbers as absolutes like ROW($1:$50)
so they do not change when filled down. This is unnecessary here as the "1:" is text and will not change when the copied/filled to another location.
edited Nov 30 '14 at 20:45
answered Nov 30 '14 at 20:04
JeepedJeeped
1,249613
1,249613
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
add a comment |
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
1
1
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Can you add a couple of sentences to your answer to describe the gist of how the formula works?
– fixer1234
Nov 30 '14 at 20:08
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Thanks! It gives the position, but that's good enough!
– Gary's Student
Nov 30 '14 at 20:33
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
Amazing and fast Excel-fu. +1
– agtoever
Nov 30 '14 at 21:17
add a comment |
Another way to determine the position
Column B
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
This returns the position of the first letter found. Returns Len(A1) + 1 in case no letter is found.
The first argument of Find is an array of all letters to find in the string. The second argument is the actual string in upper case along with all letters at the end so that the find function always returns a value for all the letters.
Column C
=IF(LEN(A1) >= B1, RIGHT(LEFT(A1, B1),1), "")
This returns the letter found in the string. In case the string has no letter, it would return empty.
Reference:
https://exceljet.net/formula/split-text-and-numbers
The link provides a solution for numbers. The above formula is switched for letters.
add a comment |
Another way to determine the position
Column B
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
This returns the position of the first letter found. Returns Len(A1) + 1 in case no letter is found.
The first argument of Find is an array of all letters to find in the string. The second argument is the actual string in upper case along with all letters at the end so that the find function always returns a value for all the letters.
Column C
=IF(LEN(A1) >= B1, RIGHT(LEFT(A1, B1),1), "")
This returns the letter found in the string. In case the string has no letter, it would return empty.
Reference:
https://exceljet.net/formula/split-text-and-numbers
The link provides a solution for numbers. The above formula is switched for letters.
add a comment |
Another way to determine the position
Column B
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
This returns the position of the first letter found. Returns Len(A1) + 1 in case no letter is found.
The first argument of Find is an array of all letters to find in the string. The second argument is the actual string in upper case along with all letters at the end so that the find function always returns a value for all the letters.
Column C
=IF(LEN(A1) >= B1, RIGHT(LEFT(A1, B1),1), "")
This returns the letter found in the string. In case the string has no letter, it would return empty.
Reference:
https://exceljet.net/formula/split-text-and-numbers
The link provides a solution for numbers. The above formula is switched for letters.
Another way to determine the position
Column B
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
This returns the position of the first letter found. Returns Len(A1) + 1 in case no letter is found.
The first argument of Find is an array of all letters to find in the string. The second argument is the actual string in upper case along with all letters at the end so that the find function always returns a value for all the letters.
Column C
=IF(LEN(A1) >= B1, RIGHT(LEFT(A1, B1),1), "")
This returns the letter found in the string. In case the string has no letter, it would return empty.
Reference:
https://exceljet.net/formula/split-text-and-numbers
The link provides a solution for numbers. The above formula is switched for letters.
answered Sep 19 '16 at 5:37
Raghav TandonRaghav Tandon
1313
1313
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f846822%2ffind-first-letter-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
SuperUser is not a "Please write me a script" site. What have you tried so far?
– LPChip
Nov 30 '14 at 19:23
@LPChip Sorry........see my EDIT#1
– Gary's Student
Nov 30 '14 at 19:37