T
Thomas M.
Excel 2007
Is there a way to find the position of the first digit within a text string?
--Tom
Is there a way to find the position of the first digit within a text string?
--Tom
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Thomas M. said:Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom
Does assume though that there actually is a number in there somewhere.
Ken Wright said:Does assume though that there actually is a number in there somewhere. If there isn't then an erroneous answer will be returned.
Likewise if the cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Thomas M. said:Excel 2007
Is there a way to find the position of the first digit within a text string?
--Tom
macropod said:Hi Ken,
Does assume though that there actually is a number in there somewhere.
Yep - I didn't bother with error checking, because it wasn't asked for. I
do agree, though, that it's generally a good idea to do so and your
approach does indeed seem to have all possibilities covered.
--
Cheers
macropod
[Microsoft MVP - Word]
Ken Wright said:Does assume though that there actually is a number in there somewhere. If
there isn't then an erroneous answer will be returned. Likewise if the
cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom
Ken Wright said:Does assume though that there actually is a number in there somewhere. If
there isn't then an erroneous answer will be returned. Likewise if the
cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Thomas M. said:Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom
Thomas M. said:That works great. I'm a little surprised that there doesn't appear to be
a built-in function for this. I was expecting, for example, something
like a variation on the FIND or SEARCH functions that would essentially
direct the function to return the position of the first digit in a string
value.
In my particular situation the text string will always contain a number,
but writing formulas to cover as many situations as is reasonably possible
is always the best approach.
Thanks for the help.
--Tom
Ken Wright said:Does assume though that there actually is a number in there somewhere. If
there isn't then an erroneous answer will be returned. Likewise if the
cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom
Thomas M. said:That works great. I'm a little surprised that there doesn't appear to be a
built-in function for this. I was expecting, for example, something like a
variation on the FIND or SEARCH functions that would essentially direct the
function to return the position of the first digit in a string value.
In my particular situation the text string will always contain a number, but
writing formulas to cover as many situations as is reasonably possible is
always the best approach.
Thanks for the help.
--Tom
Ken Wright said:Does assume though that there actually is a number in there somewhere. If
there isn't then an erroneous answer will be returned. Likewise if the
cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
macropod said:Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom
macropod said:Hi Thomas,
You could build your own function to do this. For example:
Function FindDigit(Str As String) As Integer
Dim Pos As Integer, i As Integer
For i = 0 To 9
If InStr(Str, i) > 0 Then
If Pos > 0 Then
Pos = Application.WorksheetFunction.Min(Pos, InStr(Str, i))
Else
Pos = InStr(Str, i)
End If
End If
Next
FindDigit = Pos
End Function
If you place the above function into a standard vba module for the
workbook, you can use a formula like:
=FindDigit(A1)
to retrieve the first digit's position, or '0' if there are no digits in
the string.
--
Cheers
macropod
[Microsoft MVP - Word]
Thomas M. said:That works great. I'm a little surprised that there doesn't appear to be
a built-in function for this. I was expecting, for example, something
like a variation on the FIND or SEARCH functions that would essentially
direct the function to return the position of the first digit in a string
value.
In my particular situation the text string will always contain a number,
but writing formulas to cover as many situations as is reasonably
possible is always the best approach.
Thanks for the help.
--Tom
Ken Wright said:Does assume though that there actually is a number in there somewhere.
If there isn't then an erroneous answer will be returned. Likewise if
the cell were blank, a 1 will be returned.
=IF(MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))>LEN(A1),0,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")))
Will cover all eventualities I think.
Regards
Ken.........................
Hi Thomas,
Try:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))
where the string you're testing is in A1.
--
Cheers
macropod
[Microsoft MVP - Word]
Excel 2007
Is there a way to find the position of the first digit within a text
string?
--Tom