Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Retrieve number from string

Hi

I'm gtrying to retrieve a number from a string. The string is a cell value,
that can be

AAAAAA 12 AAA
AA12
12AA
1A2AAA12
AAAA 12 AAAA

or any other combination of letters and numbers. I need at function, that
rerieves only the numbers. The results of above should be

12
12
12
1212
12

Regards
Jan


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Retrieve number from string

Function GetNum(Target As Range)

InputData = Target.Value
GetNum = ""
Do While InputData < ""
If Left(InputData, 1) = "0" And _
Left(InputData, 1) <= "9" Then

GetNum = GetNum & Left(InputData, 1)
End If

InputData = Mid(InputData, 2)

Loop

End Function


"Jan Kronsell" wrote:

Hi

I'm gtrying to retrieve a number from a string. The string is a cell value,
that can be

AAAAAA 12 AAA
AA12
12AA
1A2AAA12
AAAA 12 AAAA

or any other combination of letters and numbers. I need at function, that
rerieves only the numbers. The results of above should be

12
12
12
1212
12

Regards
Jan



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Retrieve number from string

On Mon, 21 Jul 2008 12:26:25 +0200, "Jan Kronsell"
wrote:

Hi

I'm gtrying to retrieve a number from a string. The string is a cell value,
that can be

AAAAAA 12 AAA
AA12
12AA
1A2AAA12
AAAA 12 AAAA

or any other combination of letters and numbers. I need at function, that
rerieves only the numbers. The results of above should be

12
12
12
1212
12

Regards
Jan



Enter the UDF below.
<alt-F11 opens the VB Editor
Ensure your project is highlighted in the project explorer window, then
Insert/Module and paste the code below into the window that opens.

You can then use the formula: =ReSub(cell_ref, Pattern) to "remove" what you
don't want (this is more efficient than returning what you do.

=resub(A1,"\D+") will remove everything that is NOT a digit, returning only
digits.

The function returns the value as Text. If you want it returned as a Numeric
value, then use:


=--ReSub(A1,"\D+")

or

=VALUE(ReSub(A1,"\D+"))



=====================================
Option Explicit
Function ReSub(str As String, sPat As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
ReSub = re.Replace(str, "")
End Function
=====================================
--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Retrieve number from string

Thank you to you both.

Jan

Ron Rosenfeld wrote:
On Mon, 21 Jul 2008 12:26:25 +0200, "Jan Kronsell"
wrote:

Hi

I'm gtrying to retrieve a number from a string. The string is a cell
value, that can be

AAAAAA 12 AAA
AA12
12AA
1A2AAA12
AAAA 12 AAAA

or any other combination of letters and numbers. I need at function,
that rerieves only the numbers. The results of above should be

12
12
12
1212
12

Regards
Jan



Enter the UDF below.
<alt-F11 opens the VB Editor
Ensure your project is highlighted in the project explorer window,
then Insert/Module and paste the code below into the window that
opens.

You can then use the formula: =ReSub(cell_ref, Pattern) to "remove"
what you don't want (this is more efficient than returning what you
do.

=resub(A1,"\D+") will remove everything that is NOT a digit,
returning only digits.

The function returns the value as Text. If you want it returned as a
Numeric value, then use:


=--ReSub(A1,"\D+")

or

=VALUE(ReSub(A1,"\D+"))



=====================================
Option Explicit
Function ReSub(str As String, sPat As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
ReSub = re.Replace(str, "")
End Function
=====================================
--ron



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
How to retrieve the value within string? Eric Excel Discussion (Misc queries) 11 April 20th 10 12:47 PM
How to retrieve certain char within a string? Eric Excel Discussion (Misc queries) 1 July 3rd 07 03:02 AM
How to retrieve certain text from string? Eric Excel Discussion (Misc queries) 5 December 3rd 06 07:48 PM
How to retrieve certain text from string? Eric Excel Worksheet Functions 2 December 3rd 06 02:06 PM
How to retrieve character from a string for excel? Eric Excel Discussion (Misc queries) 1 September 4th 06 08:20 AM


All times are GMT +1. The time now is 12:25 PM.

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

About Us

"It's about Microsoft Excel"