Return Only Numerics from String

email me

This is how to take a string of data that contains letters and numbers, and only return the number data.

I was using this to return computer model numbers…

on error resume next

Dim ObjRegExp, numStr, varStr

varStr="Elitebook 840 G1"

numStr=""
Set ObjRegExp = New RegExp
ObjRegExp.Pattern = "[W]+|[a-zA-Z]+"
ObjRegExp.IgnoreCase = True
ObjRegExp.Global = True
numStr = ObjRegExp.Replace(varStr,"")
Msgbox numStr

 

 

Update

I noticed that some models, such as 840 G1, would return 840 1. To remove the space, I added

Replace(strPostcode," ","",1,-1)

My script

Dim objWMI : Set objWMI = GetObject("winmgmts:")
Dim colSettingsComp : Set colSettings = objWMI.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colSettings
LaptopModel = Trim(objComputer.Model)
Next

'RETURN ONLY NUMERIC PORTION OF COMPUTER MODEL
numStr=""
Set objRegExp = New RegExp
objRegExp.Pattern ="[W]+|[a-zA-Z]+"
objRegExp.IgnoreCase = True
objRegExp.Global = True
numStr = objRegExp.Replace(LaptopModel,"")
numStr = Replace(numStr," ","",1,-1)'REMOVES SPACES

msgbox numStr