Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to find the first occurence of any number in a string

Having a variable length string, e.g., "Court House 133324 area 1678" or
"Hollywood Doors Space 5478 room 4", how to do I find the position of the
first numeric caracter?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default How to find the first occurence of any number in a string

This can be used in Code or in a Sheet as a user defined function... It
returns -1 if a digit is not found...

In Code
dim i as integer
i = firstnumber("Court House 133324 area 1678")

In a sheet
=FirstNumber(A1)

Public Function FirstNumber(ByVal InputString As String) As Integer
Dim intCounter As Integer
Dim intStringLength As Integer
Dim intReturnValue As Integer

intReturnValue = -1
intStringLength = Len(InputString)

For intCounter = 1 To intStringLength
If IsNumeric(Mid(InputString, intCounter, 1)) Then
intReturnValue = intCounter
Exit For
End If
Next intCounter

FirstNumber = intReturnValue
End Function
--
HTH...

Jim Thomlinson


"Caio Milani" wrote:

Having a variable length string, e.g., "Court House 133324 area 1678" or
"Hollywood Doors Space 5478 room 4", how to do I find the position of the
first numeric caracter?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to find the first occurence of any number in a string

This might be faster. It will return -1 if there is no number.

Function PositionFirstNumberInString(strString As String) As Long

Dim i As Long
Dim btArray() As Byte

btArray = strString

For i = 0 To UBound(btArray) Step 2
If btArray(i) 47 And btArray(i) < 58 Then
PositionFirstNumberInString = i \ 2 + 1
Exit Function
End If
Next

PositionFirstNumberInString = -1

End Function


RBS


"Caio Milani" wrote in message
...
Having a variable length string, e.g., "Court House 133324 area 1678" or
"Hollywood Doors Space 5478 room 4", how to do I find the position of the
first numeric caracter?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the row number of name occurence [email protected] Excel Worksheet Functions 9 October 3rd 08 08:34 AM
SUMIF where CRITERIA is looking for a string occurence in the value,not the whole value Finny Excel Worksheet Functions 3 September 5th 08 04:54 PM
Find first occurence of a number in an array 7 cols wide Ricardo-SA Excel Worksheet Functions 3 April 5th 08 11:22 PM
find last number in a string? Steve Excel Discussion (Misc queries) 3 June 12th 07 03:56 PM
Find any number within string R. Choate Excel Programming 3 September 22nd 05 03:18 AM


All times are GMT +1. The time now is 10:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"