1

I cant seem to get the syntax right for using the RegExp function in VBA this is what I have, can anyone show me where I am going wrong or some examples of usage in VBA. I don't understand how I assign the matches found by RegExp into a variable or array(). I get an Error saying wrong number of arguments when it hits the RegEx.Execute

Dim RegEx As New RegExp
Dim textOrders As String
Dim RawText as String

FileNum = FreeFile()
Open FileName For Input As #FileNum
Do Until EOF(FileNum)
Input #FileNum, ShortText

RawText = RawText & ShortText

loop

With RegEx
    .Global = True
    .MultiLine = False
    .IgnoreCase = True
    .Pattern = ("ORDER NUMBER :\s+[^\s]+\s+Ship Date:\s+[^\s]+\n\n\s+Style Desc : .+\n\s+Linecode : .+\n\s+Qty\s+.+")

End With

textOrders = RegEx.Execute(RawText)  *This is where the error occurs*

I then tried this but still no luck

Sub GetMatches(ByRef Str As String, ByRef coll As Collection)

Dim rExp As Object, rMatch As Object

Set rExp = CreateObject("vbscript.regexp")
With rExp
    .Global = True
    .Pattern = "ORDER NUMBER :\s+[^\s]+\s+Ship Date:\s+[^\s]+\n\n\s+Style Desc : .+\n\s+Linecode : .+\n\s+Qty\s+.+"
End With

Set rMatch = rExp.Execute(Str)
If rMatch.Count > 0 Then
    For Each rMatch In rMatch
        coll.Add rMatch.Value
        Debug.Print rMatch.Value
    Next rMatch
End If
Debug.Print ""
End Sub

I then tried this: in JavaScript

Public Sub x()
     Dim o As New ScriptControl

     Dim OriginalText As String
     Dim RegExPattern As String

     OriginalText = ""

     RegExPattern = ""
     Str = "ORDER NUMBER :   M0773175            Ship Date: 23-Nov-15" & _
            "Style Desc : BLACKOUT CURTAINS CR 46X54" & _
            "Linecode:        M878966" & _
            "Qty 36"

     o.Language = "JScript"
     With o
         .AddCode "Function x() {return m(0);}" & _
                    "var str = Str;" & _
                    "var re = '/ORDER NUMBER :\s+[^\s]+\s+Ship Date:\s+[^\s]+\n\n\s+Style Desc : .+\n\s+Linecode : .+\n\s+Qty\s+.+/g';" & _
                    "var m;" & _
                    "while ((m = re.exec(str)) !== null) {" & _
                        "if (m.index === re.lastIndex) {" & _
                        "re.lastIndex++;" & _
                        "};" & _
                    "};"

         Debug.Print .run("x", 1, 2)
     End With
 End Sub

Sample Text of pattern trying to find:

ORDER NUMBER :   M0773175            Ship Date: 23-Nov-15

      Style Desc : BLACKOUT CURTAINS CR 46X54
      Linecode : M85466
      Qty              36

Sample of input text to search: it contains 9 orders but could contain 50 or 10 dynamic amount each time that's why trying to use regex

Purchase Order Supplier Copy                                                                   Printed Date: 18/08/2015 3.32 PM              Page 1 of         15

  ORDER DETAILS                                                    INVOICE ADDRESS             SHIPMENT & PAYMENT TERMS   STYLE DETAILS

 DATE: 18-AUG-15                        ORDER NUMBERS:             Purchase Ledger             COM:CHINA                            STYLE DESCRIPTION             SUPPLIER STYLE REF    LINECODE
 ORDER TYPE: Standard                   M0773175   M0773177        Retail Ltd              COO:UK
 SEASON: S16                            M0773180   M0773183        Perimeter Road              Port:UK                              BLACKOUT CURTAINS CR 46X54    HRW14CT001            M878966
 DIVISION: HOMEWARE                     M0773186   M0773189        Knowsley Industrial Park    Factory:                             BLACKOUT CURTAINS CR 66X72    HRW14CT001            M878967
 DEPT: HOME FURNISHINGS                 M0773192   M0773195        Liverpool                   Buying Terms:                        BLACKOUT CURTAINS CR 90X90    HRW14CT001            M878972
 BUYER: Harpal Rai-Hilton               M0773198                   United Kingdom              Payment Terms:ENDMONTH           BLACKOUT CURTAINS GR 46X54    HRW14CT001            M878974
 SUPPLIER:                                         L33 7SZ                     Shipping Method:LAND                 BLACKOUT CURTAINS GR 66X72    HRW14CT001            M878975
 AGENT: 9999 N/A                                                   0151                        Pack Desc:Solid Boxed                BLACKOUT CURTAINS GR 90X90    HRW14CT001            M878976
                                                                                               Currency:STERL
                                                                                                                                    BLACKOUT CURTAINS BG 46X54    HRW14CT001            M878977
                                                                                               FOB value:5086.62
                                                                                                                                    BLACKOUT CURTAINS BG 66X72    HRW14CT001            M878978
                                                                                                                                    BLACKOUT CURTAINS BG 90X90    HRW14CT001            M878979

   PRODUCTION SUMMARY

   GRAND TOTAL

  ORDER NUMBER :   M0773175            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS CR 46X54
  Linecode : M878966
  Qty              36

  ORDER NUMBER :   M0773177            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS CR 66X72
  Linecode : M878967
  Qty              27

  ORDER NUMBER :   M0773180            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS CR 90X90
  Linecode : M878972
  Qty              38

  ORDER NUMBER :   M0773183            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS GR 46X54
  Linecode : M878974
  Qty              30

  ORDER NUMBER :   M0773186            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS GR 66X72
  Linecode : M878975
  Qty              15

  ORDER NUMBER :   M0773189            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS GR 90X90
  Linecode : M878976
  Qty              60

  ORDER NUMBER :   M0773192            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS BG 46X54
  Linecode : M878977
  Qty              42

  ORDER NUMBER :   M0773195            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS BG 66X72
  Linecode : M878978
  Qty              21

  ORDER NUMBER :   M0773198            Ship Date: 23-Nov-15

  Style Desc : BLACKOUT CURTAINS BG 90X90
  Linecode : M878979
  Qty              40
Erik A
  • 31,639
  • 12
  • 42
  • 67
David
  • 95
  • 8
  • 1
    `textOrders` is declared as a string, but `RegEx.Execute` will return a match object. – Wiktor Stribiżew Sep 23 '15 at 09:44
  • @stribizhev so how do I access the match object to get the blocks of text out the file? I know I may have to change the pattern It works in [https://regex101.com/](https://regex101.com/) but cant seem to use it in VBA – David Sep 23 '15 at 09:47
  • Please have a look at [this answer of mine for *Split string on single forward slashes with RegExp* post](http://stackoverflow.com/questions/32586057/split-string-on-single-forward-slashes-with-regexp/32588080#32588080). You can find `GetMatches` function that should meet your needs. If not, please let me know. – Wiktor Stribiżew Sep 23 '15 at 10:00
  • If that solution does not solve the issue you are having, I will reopen the question. – Wiktor Stribiżew Sep 23 '15 at 10:11
  • @stribizhev hi your example just confused me even more, please can you open question back up cheers – David Sep 23 '15 at 12:12
  • Only after you try my code and post what did not work for you (what errors you get). – Wiktor Stribiżew Sep 23 '15 at 12:13
  • @stribizhev I first had to change the `r_item` to `rMatch` as VBA doesn't recognise this. I changed your answer to try and reflect what I am trying to do and still nothing. – David Sep 23 '15 at 12:31
  • Please post a sample input string and expected output. Without it, it is impossible to help you. – Wiktor Stribiżew Sep 23 '15 at 12:39
  • Try `ORDER NUMBER\s*:\s*\S+\s*Ship Date:\s*\S+\s*Style Desc : .+\s*Linecode\s*: .+\s*Qty\s*.+` regex. – Wiktor Stribiżew Sep 23 '15 at 12:52
  • @stribizhev Hi that works but only finds 1 match when should find 9, [https://regex101.com](https://regex101.com) it says add g modifier to get the global matches how do I do that in your example? – David Sep 23 '15 at 13:13

1 Answers1

0

I think there might be an issue with the regex where the number of newlines (and their type) may not be as expected (\r\n or just \n?). I think it is safer to switch to just \s* that will match any optional whitespace.

The regex will look like

ORDER NUMBER\s*:\s*\S+\s*Ship Date:\s*\S+\s*Style Desc : .+\s*Linecode\s*: .+\s*Qty\s*.+

See the regex demo

Here is the code that works:

The sub that collects all matches:

Sub GetMatches2(ByRef str As String, ByRef coll As collection)

Dim rExp As Object, rMatch As Object, r_item As Object

Set rExp = CreateObject("vbscript.regexp")
With rExp
    .Global = True
    .MultiLine = False
    .IgnoreCase = True
    .pattern = "ORDER NUMBER\s*:\s*\S+\s*Ship Date:\s*\S+\s*Style Desc : .+\s*Linecode\s*: .+\s*Qty\s*.+"
End With

Set rMatch = rExp.Execute(str)
If rMatch.Count > 0 Then
    For Each r_item In rMatch
        coll.Add r_item.Value
    Next r_item
End If

End Sub

And the main sub where the string is passed to GetMatches2:

Sub ProcessStr()
Dim s As String
s = "Purchase Order Supplier Copy                                                                   Printed Date: 18/08/2015 3.32 PM              Page 1 of         15" & vbCrLf & vbCrLf & "  ORDER DETAILS                                                    INVOICE ADDRESS             SHIPMENT & PAYMENT TERMS   STYLE DETAILS" & vbCrLf & "" & vbCrLf & " DATE: 18-AUG-15                        ORDER NUMBERS:             Purchase Ledger             COM:CHINA                            STYLE DESCRIPTION             SUPPLIER STYLE REF    LINECODE" & vbCrLf & " ORDER TYPE: Standard                   M0773175   M0773177        Retail Ltd              COO:UK" & vbCrLf & " SEASON: S16                            M0773180   M0773183        Perimeter Road              Port:UK                              BLACKOUT CURTAINS CR 46X54    HRW14CT001            M878966" & vbCrLf & _
" DIVISION: HOMEWARE                     M0773186   M0773189        Knowsley Industrial Park    Factory:                             BLACKOUT CURTAINS CR 66X72    HRW14CT001            M878967" & vbCrLf & " DEPT: HOME FURNISHINGS                 M0773192   M0773195        Liverpool                   Buying Terms:                        BLACKOUT CURTAINS CR 90X90    HRW14CT001            M878972" & vbCrLf & " BUYER: Harpal Rai-Hilton               M0773198                   United Kingdom              Payment Terms:ENDMONTH           BLACKOUT CURTAINS GR 46X54    HRW14CT001            M878974" & vbCrLf & " SUPPLIER:                                         L33 7SZ                     Shipping Method:LAND                 BLACKOUT CURTAINS GR 66X72    HRW14CT001            M878975" & vbCrLf & " AGENT: 9999 N/A                                                   0151                        Pack Desc:Solid Boxed                BLACKOUT CURTAINS GR 90X90    HRW14CT001            M878976" & _
          vbCrLf & "                                                                              Currency:STERL" & vbCrLf & _
"                                                                                                                                    BLACKOUT CURTAINS BG 46X54    HRW14CT001            M878977" & vbCrLf & "                                                                                               FOB value:5086.62" & vbCrLf & "                                                                                                                                    BLACKOUT CURTAINS BG 66X72    HRW14CT001            M878978" & vbCrLf & "                                                                                                                                    BLACKOUT CURTAINS BG 90X90    HRW14CT001            M878979" & vbCrLf & vbCrLf & _
"   PRODUCTION SUMMARY" & vbCrLf & vbCrLf & "   GRAND TOTAL" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773175            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS CR 46X54" & vbCrLf & "  Linecode : M878966" & vbCrLf & "  Qty              36" & vbCrLf & vbCrLf & _
"  ORDER NUMBER :   M0773177            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS CR 66X72" & vbCrLf & "  Linecode : M878967" & vbCrLf & "  Qty              27" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773180            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & _
"  Style Desc : BLACKOUT CURTAINS CR 90X90" & vbCrLf & "  Linecode : M878972" & vbCrLf & "  Qty              38" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773183            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS GR 46X54" & vbCrLf & "  Linecode : M878974" & vbCrLf & "  Qty              30" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773186            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS GR 66X72" & vbCrLf & _
"  Linecode : M878975" & vbCrLf & "  Qty              15" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773189            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS GR 90X90" & vbCrLf & "  Linecode : M878976" & vbCrLf & "  Qty              60" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773192            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & _
"  Style Desc : BLACKOUT CURTAINS BG 46X54" & vbCrLf & "  Linecode : M878977" & vbCrLf & "  Qty              42" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773195            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS BG 66X72" & vbCrLf & _
"  Linecode : M878978" & vbCrLf & "  Qty              21" & vbCrLf & vbCrLf & "  ORDER NUMBER :   M0773198            Ship Date: 23-Nov-15" & vbCrLf & vbCrLf & "  Style Desc : BLACKOUT CURTAINS BG 90X90" & vbCrLf & _
"  Linecode : M878979" & vbCrLf & "  Qty              40"
Dim matches As collection

Set matches = New collection
GetMatches2 str:=s, coll:=matches

End Sub

Results:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563