Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Extract Numbers From String

I have a field formatted like the following:

2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2112/M&N
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR

Is there a way to extact just the numbers and if there is more than one
number, seperate it by a comma? Note: Each of the numbers are 4 digits
in length.

ex - first row should read 2340, 2341

TIA!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Extract Numbers From String


BerkshireGuy wrote:
I have a field formatted like the following:

2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2112/M&N
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR

Is there a way to extact just the numbers and if there is more than one
number, seperate it by a comma? Note: Each of the numbers are 4 digits
in length.

ex - first row should read 2340, 2341

TIA!


Hi,

Try this user defined function (requires XL2000 or higher)...

Public Function Numbers(Cell As String) As String
Cell = WorksheetFunction.Substitute(Cell, "/", " ")
Dim StrArray As Variant, I As Long
StrArray = Split(Cell)
For I = 0 To UBound(StrArray)
If IsNumeric(StrArray(I)) Then
Numbers = Numbers & StrArray(I) & ", "
End If
Next I
If Len(Numbers) 2 Then
Numbers = Left(Numbers, Len(Numbers) - 2)
End If
End Function

Ken Johnson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Extract Numbers From String

Try

Function ExtractNums(ByVal InString As String) As Variant
Dim i As Long
Dim A As Variant
Dim ch As String
For i = 1 To Len(InString)
ch = Mid(InString, i, 1)
If Asc(ch) < 48 Or Asc(ch) 57 Then
Mid(InString, i, 1) = ","
End If
Next i
Do While InStr(InString, ",,") < 0
InString = Replace(InString, ",,", ",")
Loop
If InString Like "*," Then
InString = Mid(InString, 1, Len(InString) - 1)
End If
If InString Like ",*" Then
InString = Mid(InString, 2)
End If
ExtractNums = Replace(InString, ",", ", ")
End Function


BerkshireGuy wrote:
I have a field formatted like the following:

2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2112/M&N
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR

Is there a way to extact just the numbers and if there is more than one
number, seperate it by a comma? Note: Each of the numbers are 4 digits
in length.

ex - first row should read 2340, 2341

TIA!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Extract Numbers From String

One mo

Option Explicit
Function JustNumbers(rng As Range)

Dim iCtr As Long
Dim myStr As String

myStr = rng.Cells(1).Text

For iCtr = 1 To Len(myStr)
If IsNumeric(Mid(myStr, iCtr, 1)) Then
'do nothing
Else
Mid(myStr, iCtr, 1) = " "
End If
Next iCtr

With Application
myStr = .Substitute(.Trim(myStr), " ", ", ")
End With

JustNumbers = myStr

End Function

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Short course:

Open your workbook.
Hit alt-f11 to get to the VBE (where macros/UDF's live)
hit ctrl-R to view the project explorer
Find your workbook.
should look like: VBAProject (yourfilename.xls)

right click on the project name
Insert, then Module
You should see the code window pop up on the right hand side

Paste the code in there.

Now go back to excel.
Into a test cell and type:
=JustNumbers(a1)

And it won't care how many consecutive digits there are in any number.


BerkshireGuy wrote:

I have a field formatted like the following:

2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2340/LEFT EYE BLINDNESS, 2341/CAP PRES-
2112/M&N
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
2112 MENTAL/NERVOUS, 2801 ASTHMA_______
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR
REIS:ADD 2960/THORACIC, 2918/EITHER OR

Is there a way to extact just the numbers and if there is more than one
number, seperate it by a comma? Note: Each of the numbers are 4 digits
in length.

ex - first row should read 2340, 2341

TIA!


--

Dave Peterson
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
Extract numbers from a string of text Huber57 Excel Discussion (Misc queries) 0 May 27th 10 09:53 PM
HOW DO I EXTRACT NUMBERS FROM TEXT STRING AndyF Excel Discussion (Misc queries) 7 August 14th 08 02:37 PM
extract numbers from a string Dave Excel Worksheet Functions 3 August 7th 08 09:43 PM
extract numbers from text string thomsonpa New Users to Excel 4 December 17th 07 11:02 AM
Extract numbers from a string ? Jello Excel Programming 6 March 3rd 05 10:27 PM


All times are GMT +1. The time now is 06:45 AM.

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"