View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.misc
Corey
 
Posts: n/a
Default Extract Numerics only

Thanks for the suggestion Dave. I tried your VBA code and it gets hung up on
the "Next K" portion. ???



"Dave O" wrote:

The only way I can think to do this is with VBA code. This code works
on a column of numbers, and will place the numeric-only string in the
column immediately to the right of the original column. Type the word
"stop" (no quotes) in the cell under the last alpha-numeric cell you
want to evaluate.

Note- this code formats the cells that will hold the numeric-only
values as text to preserve any leading zeroes. See the comments within
the code.

Sub RemoveText()
Dim Werd As String, NewWerd As String
Dim K As Byte

Do Until ActiveCell.Value = "stop" 'start main loop
Werd = ActiveCell.Value 'store part number to memory
For K = 1 To Len(Werd) 'strip out non-numeric characters
If Asc(Mid(Werd, K, 1)) = 48 And Asc(Mid(Werd, K, 1)) <= 57 Then
NewWerd = NewWerd & Mid(Werd, K, 1)
Next K

ActiveCell.Offset(0, 1).Select 'move to adjacent column
Selection.NumberFormat = "@" 'format cell as text to preserve
leading zero, if any
ActiveCell.Value = NewWerd 'apply numeric value
ActiveCell.Offset(0, -1).Select 'move back
NewWerd = "" 'set value to nothing
ActiveCell.Offset(1, 0).Select 'move down one cell
Loop

End Sub