VBA - Looking to strip alphanumeric from numeric

D

Dingo

Hi,

I am trying to write a VBA macro that given an input of, say
"AS9F89A779FA87" will give an output of "98977987". The macro I want t
write would analyze a given input with mixed alphanumeric and numeri
characters, and remove the alphanumeric from the numeric.

But I'm stuck....help please
 
C

Chip Pearson

Try something like the following:


Dim S As String
Dim Pos As Long
Dim Res As String
S = "ABC123DEF456"
For Pos = 1 To Len(S)
If IsNumeric(Mid(S, Pos, 1)) = True Then
Res = Res & Mid(S, Pos, 1)
End If
Next Pos
MsgBox Res


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
G

Gord Dibben

Dingo

Sub RemoveAlphas()
'' Remove alpha characters from a string.
Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String

Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues)

For Each rngR In rngRR
strTemp = ""
For intI = 1 To Len(rngR.Value)
If Mid(rngR.Value, intI, 1) Like "[0-9]" Then
strNotNum = Mid(rngR.Value, intI, 1)
Else: strNotNum = ""
End If
strTemp = strTemp & strNotNum
Next intI
rngR.Value = strTemp
Next rngR

End Sub

Gord Dibben Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top