Help using .Pattern = ??

walkinginheels24

New Member
Joined
Jun 28, 2019
Messages
3
I am trying to find a macro so I can extract a period number from a string of text e.g. P01 20
I have been using the following macro that I found on here, that I have edited however I cannot figure out how to include the space (between P01 and 20) to be included in the pattern line

Function Period(sInp As String) As String()
Dim oRE As Object
Dim oMat As Object
Dim asOut() As String
Dim iMat As Long

Set oRE = CreateObject("vbscript.regexp")
With oRE
.Global = True
.IgnoreCase = False
.Pattern = "(\b([P]{1}\d{2})\b)|"

If .Test(sInp) Then
Set oMat = .Execute(sInp)
ReDim asOut(0 To oMat.Count)
For iMat = 0 To oMat.Count - 1
asOut(iMat) = oMat(iMat)
Next iMat
End If
End With

Period = asOut
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi
Welcome to the board

Not clear to me.

Is the pattern the letter P followed by 2 digits, followed by a space and followed by 2 digits?

Is there just 1 of these in the text or more than one?

Plese post an example. The input text and want you want to be the output text.
 
Last edited:
Upvote 0
Hi there
The input text for example would be
“XY P01 20 Washer charges 1/04/2019”
And the output I would like to extract is P01 20.

Yes the pattern will either be P followed by two digits followed by a space then two digits pr P followed by one digit then a space then two digits.
 
Upvote 0
With your example try:

Code:
Function GetCode(s As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "\bP\d{1,2} \d\d\b"
        If .test(s) Then GetCode = .Execute(s)(0)
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,417
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top