Here's how I did a regex replace to extract the number(s) from a cell:
Function REGREP(value As Range, searchString As String, replaceString As String,
Optional ignoreCase As Boolean = True, Optional searchGlobal As Boolean = True)
'Dim reg As New RegExp
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.ignoreCase = ignoreCase
reg.Global = searchGlobal
reg.Pattern = searchString
REGREP = reg.replace(value.value, replaceString)
End Function
Function ASNUM(value As Range)
ASNUM = REGREP(value, "[^0-9.]+", "")
End Function
'Usage:
REGREP(A1, "[^0-9.]+", "")
ASNUM(A1)
Note that you first need to open up Visual Basic inside Excel using Alt + F11
. Then click Tools -> References
, scroll down to Microsoft VBScript Regular Expressions 5.5
, and add it in. Then RegExp
shows up when you type it.
You need to save the file as an Excel Macro-Enabled Workbook (*.xlsm), in order to keep the macro... You can also export and import your code Module.