Home » excel » excel – Extract English letters and numbers using regex

excel – Extract English letters and numbers using regex

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have the following code that extracts the English letters from mixed English and Arabic letters

Sub Test()
Dim a           As Variant
Dim i           As Long

With Cells(1).CurrentRegion.Resize(, 3)
    a = .Value
    With CreateObject("VBScript.RegExp")
        .Global = True
        For i = 1 To UBound(a, 1)
            .Pattern = "[^\w_ ]+"
            a(i, 3) = Trim$(.Replace(a(i, 1), ""))
        Next i
    End With
    .Value = a
End With
End Sub

I need to change the pattern so as to be able to extract the numbers too .. so I need to extract the English letters and also the numbers.

How to&Answers:

Add \d after \w . This allows for extraction of numbers if the numbers are between the Arabic letters.

From comment by @Tiw / OP.