2

How can I extract the numbers from col A and print in into col B.

I am using the below regex function, it print all the numbers with a space between them.

How can I get the initial set of numbers and skip the remaining ones.

Docetaxel Injection 160MG/16ML prints 160 16. I need to print only 160.

Private Sub splitUpRegexPattern()
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim Myrange As Range

    Set Myrange = ActiveSheet.Range("A1:A10")

    For Each C In Myrange
        strPattern = "\D+"

        If strPattern <> "" Then
            strInput = C.Value
            strReplace = "$1"

            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
            End With

            If regEx.test(strInput) Then
                C.Offset(0, 1) = regEx.Replace(strInput, " ")
            Else
                C.Offset(0, 1) = "(Not matched)"
            End If
        End If
    Next
End Sub
6
  • Instead of using Regex, can you just search for a space, and use =left? Commented Jan 22, 2018 at 23:32
  • But can I extract just the numbers without using regex? Commented Jan 22, 2018 at 23:35
  • Probably not - I was under the impression from the example you gave that your data is just numbers morenumbers - is this incorrect? Commented Jan 22, 2018 at 23:36
  • With the regex function it is extracting just numbers. E.g. Docetaxel Injection 160MG/16ML prints 160 16. I am trying to print just the digits before the space, like just 160 and not 160 16 in the new column Commented Jan 22, 2018 at 23:42
  • Is it always 3 digits? Commented Jan 23, 2018 at 0:16

2 Answers 2

2

This should work (pattern allows for decimals but not very robustly so):

Sub splitUpRegexPattern()

    Dim re As Object, c As Range
    Dim allMatches

    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "([\d+\.]+)"
    re.IgnoreCase = True
    re.Global = True

    For Each c In ActiveSheet.Range("A1:A10").Cells
        Set allMatches = re.Execute(c.Value)
        If allMatches.Count > 0 Then
            c.Offset(0, 1).Value = allMatches(0)
        Else
            c.Offset(0, 1).Value = "(Not matched)"
        End If
    Next c

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

This solution does not print the numbers, everything except the numbers, like PRECEDEX 200 mcg 2 mL FTV will get printed as PRECEDEX mcg mL FTV
So what you're saying is you didn't even try it?
Below is what I did to print just the first set If regEx.test(strInput) Then C.Offset(0, 1) = regEx.Exceute(strInput)(0) Else C.Offset(0, 1) = "(Not matched)" End If
Yes, it stops when it finds the first " . " like in Docetaxel I.V. Infusion 120mg/12mL Hospira it prints . and not 120
You can just remove the \. if you don't care about decimals. Or find a more-robust pattern...
1

If its always 3 digits then use \s\d{3} https://regex101.com/r/lEc4mN/1

enter image description here

Option Explicit
Private Sub splitUpRegexPattern()
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim Myrange As Range
    Dim C As Range
    Dim Matches As Variant

    Set Myrange = ActiveSheet.Range("A1:A10")

    For Each C In Myrange
        strPattern = "\s\d{3}"

        If strPattern <> "" Then
            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
                 Set Matches = .Execute(C.Value)
            End With

            If Matches.Count > 0 Then
                Debug.Print Matches(0)
                C.Offset(0, 1) = Matches(0)
            Else
                C.Offset(0, 1) = "(Not matched)"
                Debug.Print "Not Found "
            End If

        End If
    Next
End Sub

2 Comments

I am trying to extract the first set of numbers till the space. PRECEDEX 200 mcg 2 mL FTV should print only 200 and not 200 2.
@user9230639 it should work- also try to C.Offset(0, 1) = Trim(Matches(0))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.