VBA for removing extra characters in Barcode scanning

2KGrafix

New Member
Joined
Jan 26, 2024
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
So I created an Excel inventory sheet I would like to scan existing bar codes (part numbers) into. Some codes are QR (data matrix), and some standard industrial bar codes you see on a box of cereal. The issue I'm having is data matrix codes give too much data and standard bar codes either contain spaces or are missing a prefix for some components. For example :

The correct part number is: PCD6MD17E303SBW (All correct part numbers are listed in sheet2 in this format)
Data Matrix Barcode Input: PD6MD 17E30 3SBW S17867999 R7/27/1999 22:30:45 (Missing the C in the prefix which is necessary and contains spaces and extra unwanted data)
Standard Barcode Input: D6MD 17E30 3SBW (Part missing PC prefix altogether and contains spaces)

I have an HTML coding background but not great at writing VBA macros from scratch. I welcome any assistance in finding the right solution.
 
I did get a chance to test the VBA you provided. What I did notice is I do not have "= barcode" as a function. I do have a Code128 barcode font. Not sure if that matters. See screenshot.

1706632640435.png
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I may be way off on thinking here, but I think trying to account for any variable that could possible exist, through coding, is a bad idea. Whenever a new variable pops up, you would have to change the code to accommodate this "new" scenario.
Let me suggest an alternative method, which up front may require more work, but in the long run, will be simpler to accommodate new scenarios that don't fit previous patterns.
Since you have all the barcodes, and know what they should read as, add a "cross reference" table to your sheet, and reference that when scanning a barcode. then when a new, previously unidentified scenario pops up, you just have to add it to your "cross reference" database.

Book1
ABC
1Correct PartData Matrix Standard Barcode Input
2PCD6MD17E303SBWPD6MD 17E30 3SBW S17867999 R7/27/1999 22:30:45D6MD 17E30 3SBW
3
4
5
6
7
Sheet1
 
Upvote 0
I may be way off on thinking here, but I think trying to account for any variable that could possible exist, through coding, is a bad idea. Whenever a new variable pops up, you would have to change the code to accommodate this "new" scenario.
Let me suggest an alternative method, which up front may require more work, but in the long run, will be simpler to accommodate new scenarios that don't fit previous patterns.
Since you have all the barcodes, and know what they should read as, add a "cross reference" table to your sheet, and reference that when scanning a barcode. then when a new, previously unidentified scenario pops up, you just have to add it to your "cross reference" database.

Book1
ABC
1Correct PartData Matrix Standard Barcode Input
2PCD6MD17E303SBWPD6MD 17E30 3SBW S17867999 R7/27/1999 22:30:45D6MD 17E30 3SBW
3
4
5
6
7
Sheet1
Thank you and I understand what you're getting at but for now, Pete has been great at helping me solve this issue. I'm going to wait for him, but I will keep your suggestion in mind.
 
Upvote 0
@2KGrafix
I updated my functions an created a table to visualize the results:

barcode_scan.xlsm
ABCDE
1MLDLB5B17F775BE5YZ9, 22:57:38 1/26/2024, SEQ 224MLDLB5B17F775BE5YZ9MLDLB5B17F775BE5YZ9TRUE
2MLDLC5B17F954BE5YBT, 11:52: 2 1/22/2024, SEQ 257MLDLC5B17F954BE5YBTMLDLC5B17F954BE5YBTTRUE
3PLB5B 13B414 AB S10155600 D7/6/1998 0:22:1PCLB5B13B414ABPCLB5B13B414ABTRUE
4PPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWTRUE
5LB5B-8200-EB5UAWPCLB5B8200EB5UAWPCLB5B8200EB5UAWTRUE
6PCLC5B-15A227-ACPCLC5B15A227ACPCLC5B15A227ACTRUE
7PLC5B 17A848 AB S10167471 D1/15/2024 8:48:9 12345PCLC5B17A848ABPCLC5B17A848ABTRUE
8PPCLB5B17C831AE5KBXPCLC5B17C831AE5KBXPCLB5B17C831AE5KBXFALSEWrong result! "PCLC" should be "PPCLB"
9PPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMTRUE
10PCLC5B17E911AASMASPCLC5B17E911AASMASPCLC5B17E911AASMASTRUE
11PCLC5B17E911CA59B8 10324PCLC5B17E911CA59B8PCLC5B17E911CA59B8TRUE
12PPCLC5B17F001AH51MDPCLC5B17F001AH51MDPCLC5B17F001AH51MDTRUE
13PPCLC5B17K945AD59B8PCLC5B17K945AD59B8PCLC5B17K945AD59B8TRUE
14PCLC5B8200BESMA4|240238352706||20240123181548PCLC5B8200BESMA4PCLC5B8200BESMA4TRUE
15LC5B-8200-DE5KW9PCLC5B8200DE5KW9PCLC5B8200DE5KW9TRUE
16L1MT-13A803-ABPCL1MT13A803ABPCL1MT13A803ABTRUE
17L1M3-8B273-BAF,EE0WA,24010,18:10:12PCL1M38B273BAFPCL1M38B273BAFTRUE
18L1M38005BF7482PCL1M38005BFN/AFALSEno pattern possible: extra "7482" at the end
19PPCMB5J17F771AA51MDPCMB5J17F771AA51MDPCMB5J17F771AA51MDTRUE
20M1M3-8W005-AAA C757LPCM1M38W005AAAPCM1M38W005AAATRUE
21P1MT-15K867-BBAPCP1MT15K867BBAPCP1MT15K867BBATRUE
22P1M38C607FAPCP1M38C607FAPCP1M38C607FATRUE
23P1MT 14N139 BADBPCP1MT14N139BADBPCP1MT14N139BADBTRUE
24MB5B13W030JG240125045754PCMB5B13W030JGN/AFALSEcurrent patterns don't match the extra length "240125045754" at the end
25P1MT-14A303-BBCAPCP1MT14A303BBCAPCP1MT14A303BBCATRUE
26RB5B-8200-DBSMA4|0001|P3340|YYYYMMDDHHMMSS|PCRB5B8200DBSMA4PCRB5B8200DBSMA4TRUE
27CRC5B8200CBSMAS111623 ~233205437218~FD834.1A00.CA11PCRC5B8200CBSMASN/AFALSEno pattern possible: 1) extra "C" at the beginning; 2) extra "111623" at the end
28RB5B13W020CG RH SAE HIGH U625(2) 12/05/23 16:36:55PCRB5B13W029CGPCRB5B13W020CGFALSEWrong result! number "29" instead of "20"
29PPCRC5B17C831BC53CCPCRC5B17C831BB53CCPCRC5B17C831BC53CCFALSEWrong result! "BB53" should be "BC53" at the end
30RC5B13E014AEPCRC5B13E014AEPCRC5B13E014AETRUE
31PPCRB5B17F765BA5JP4PCRB5B17F765BA5JP4PCRB5B17F765BA5JP4TRUE
32
33total right guesses25
34right guess rate81%
Sheet2
Cell Formulas
RangeFormula
C1:C31C1= barcode(A1)
D1:D31D1=C1=B1
D33D33= COUNTIF(D1:D31,TRUE)
D34D34= D33/COUNTA(D1:D31)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D1:D31Cell Value=FALSEtextNO
D1:D31Cell Value=TRUEtextNO
A1:C31Expression=$D1=FALSEtextNO


Here's the VBA code you should just paste into a (new) module:

VBA Code:
Option Explicit

Private Function barcode(text As String) As String
   Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
   Dim temp As String
   Dim pattern10 As String: pattern10 = "(MLD[A-Z0-9]+),.*"
   Dim pattern20 As String: pattern20 = "(PC[A-Z0-9]+)\|.*"
   Dim pattern30 As String: pattern30 = "^([A-Z0-9]{12}) .*"
   Dim pattern40 As String: pattern40 = "^([A-Z0-9]{10,12})$"
   Dim pattern50 As String: pattern50 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)"
   Dim pattern60 As String: pattern60 = "([A-Z0-9]{4})-([A-Z0-9]+)-([A-Z0-9]+)"
   Dim pattern70 As String: pattern70 = "^(PC[A-Z0-9]+)$"
   Dim pattern80 As String: pattern80 = "^(PC[A-Z0-9]+) [A-Z0-9]+"
   Dim pattern90 As String: pattern90 = "^P(PC[A-Z0-9]+)$"
  
   Select Case True
      Case RegExpTest(text, pattern10)
         text = RegExpReplace(text, pattern10, "$1")
         barcode = text
      Case RegExpTest(text, pattern20)
         text = RegExpReplace(text, pattern20, "$1")
         barcode = text
      Case RegExpTest(text, pattern30)
         text = RegExpGet(text, pattern30)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = "PC" & text
      Case RegExpTest(text, pattern40)
         text = RegExpGet(text, pattern40)
         barcode = "PC" & text
      Case RegExpTest(text, pattern50)
         text = RegExpGet(text, pattern50)
         text = RegExpReplace(text, "([A-Z0-9]+) ([A-Z0-9]+) ([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern60)
         text = RegExpGet(text, pattern60)
         text = RegExpReplace(text, "([A-Z0-9]+)-([A-Z0-9]+)-([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern70)
         barcode = RegExpReplace(text, pattern70, "$1")
      Case RegExpTest(text, pattern80)
         text = RegExpGet(text, pattern80)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = text
      Case RegExpTest(text, pattern90)
         text = RegExpGet(text, pattern90)
         text = RegExpReplace(text, "^P(PC[A-Z0-9]+)$", "$1")
         barcode = text
      Case Else
         barcode = "N/A"
   End Select
End Function

Private Function RegExpTest(txt As String, pat As String) As Boolean
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   If rex.test(txt) = True Then
      RegExpTest = True
   Else
      RegExpTest = False
   End If
End Function

Private Function RegExpReplace(txt As String, pat As String, rep As String) As String
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = True
   RegExpReplace = rex.Replace(txt, rep)
End Function

Private Function RegExpGet(txt As String, pat As String)
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = False
   RegExpGet = rex.Execute(txt)(0)
End Function

As you can see I got a 81% success rate

I suggest you test my function with your whole data and check what "success rate" you get.

Then please let me know how we should proceed or how my function should be modified.
 
Upvote 0
@2KGrafix
I updated my functions an created a table to visualize the results:

barcode_scan.xlsm
ABCDE
1MLDLB5B17F775BE5YZ9, 22:57:38 1/26/2024, SEQ 224MLDLB5B17F775BE5YZ9MLDLB5B17F775BE5YZ9TRUE
2MLDLC5B17F954BE5YBT, 11:52: 2 1/22/2024, SEQ 257MLDLC5B17F954BE5YBTMLDLC5B17F954BE5YBTTRUE
3PLB5B 13B414 AB S10155600 D7/6/1998 0:22:1PCLB5B13B414ABPCLB5B13B414ABTRUE
4PPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWTRUE
5LB5B-8200-EB5UAWPCLB5B8200EB5UAWPCLB5B8200EB5UAWTRUE
6PCLC5B-15A227-ACPCLC5B15A227ACPCLC5B15A227ACTRUE
7PLC5B 17A848 AB S10167471 D1/15/2024 8:48:9 12345PCLC5B17A848ABPCLC5B17A848ABTRUE
8PPCLB5B17C831AE5KBXPCLC5B17C831AE5KBXPCLB5B17C831AE5KBXFALSEWrong result! "PCLC" should be "PPCLB"
9PPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMTRUE
10PCLC5B17E911AASMASPCLC5B17E911AASMASPCLC5B17E911AASMASTRUE
11PCLC5B17E911CA59B8 10324PCLC5B17E911CA59B8PCLC5B17E911CA59B8TRUE
12PPCLC5B17F001AH51MDPCLC5B17F001AH51MDPCLC5B17F001AH51MDTRUE
13PPCLC5B17K945AD59B8PCLC5B17K945AD59B8PCLC5B17K945AD59B8TRUE
14PCLC5B8200BESMA4|240238352706||20240123181548PCLC5B8200BESMA4PCLC5B8200BESMA4TRUE
15LC5B-8200-DE5KW9PCLC5B8200DE5KW9PCLC5B8200DE5KW9TRUE
16L1MT-13A803-ABPCL1MT13A803ABPCL1MT13A803ABTRUE
17L1M3-8B273-BAF,EE0WA,24010,18:10:12PCL1M38B273BAFPCL1M38B273BAFTRUE
18L1M38005BF7482PCL1M38005BFN/AFALSEno pattern possible: extra "7482" at the end
19PPCMB5J17F771AA51MDPCMB5J17F771AA51MDPCMB5J17F771AA51MDTRUE
20M1M3-8W005-AAA C757LPCM1M38W005AAAPCM1M38W005AAATRUE
21P1MT-15K867-BBAPCP1MT15K867BBAPCP1MT15K867BBATRUE
22P1M38C607FAPCP1M38C607FAPCP1M38C607FATRUE
23P1MT 14N139 BADBPCP1MT14N139BADBPCP1MT14N139BADBTRUE
24MB5B13W030JG240125045754PCMB5B13W030JGN/AFALSEcurrent patterns don't match the extra length "240125045754" at the end
25P1MT-14A303-BBCAPCP1MT14A303BBCAPCP1MT14A303BBCATRUE
26RB5B-8200-DBSMA4|0001|P3340|YYYYMMDDHHMMSS|PCRB5B8200DBSMA4PCRB5B8200DBSMA4TRUE
27CRC5B8200CBSMAS111623 ~233205437218~FD834.1A00.CA11PCRC5B8200CBSMASN/AFALSEno pattern possible: 1) extra "C" at the beginning; 2) extra "111623" at the end
28RB5B13W020CG RH SAE HIGH U625(2) 12/05/23 16:36:55PCRB5B13W029CGPCRB5B13W020CGFALSEWrong result! number "29" instead of "20"
29PPCRC5B17C831BC53CCPCRC5B17C831BB53CCPCRC5B17C831BC53CCFALSEWrong result! "BB53" should be "BC53" at the end
30RC5B13E014AEPCRC5B13E014AEPCRC5B13E014AETRUE
31PPCRB5B17F765BA5JP4PCRB5B17F765BA5JP4PCRB5B17F765BA5JP4TRUE
32
33total right guesses25
34right guess rate81%
Sheet2
Cell Formulas
RangeFormula
C1:C31C1= barcode(A1)
D1:D31D1=C1=B1
D33D33= COUNTIF(D1:D31,TRUE)
D34D34= D33/COUNTA(D1:D31)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D1:D31Cell Value=FALSEtextNO
D1:D31Cell Value=TRUEtextNO
A1:C31Expression=$D1=FALSEtextNO


Here's the VBA code you should just paste into a (new) module:

VBA Code:
Option Explicit

Private Function barcode(text As String) As String
   Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
   Dim temp As String
   Dim pattern10 As String: pattern10 = "(MLD[A-Z0-9]+),.*"
   Dim pattern20 As String: pattern20 = "(PC[A-Z0-9]+)\|.*"
   Dim pattern30 As String: pattern30 = "^([A-Z0-9]{12}) .*"
   Dim pattern40 As String: pattern40 = "^([A-Z0-9]{10,12})$"
   Dim pattern50 As String: pattern50 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)"
   Dim pattern60 As String: pattern60 = "([A-Z0-9]{4})-([A-Z0-9]+)-([A-Z0-9]+)"
   Dim pattern70 As String: pattern70 = "^(PC[A-Z0-9]+)$"
   Dim pattern80 As String: pattern80 = "^(PC[A-Z0-9]+) [A-Z0-9]+"
   Dim pattern90 As String: pattern90 = "^P(PC[A-Z0-9]+)$"
 
   Select Case True
      Case RegExpTest(text, pattern10)
         text = RegExpReplace(text, pattern10, "$1")
         barcode = text
      Case RegExpTest(text, pattern20)
         text = RegExpReplace(text, pattern20, "$1")
         barcode = text
      Case RegExpTest(text, pattern30)
         text = RegExpGet(text, pattern30)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = "PC" & text
      Case RegExpTest(text, pattern40)
         text = RegExpGet(text, pattern40)
         barcode = "PC" & text
      Case RegExpTest(text, pattern50)
         text = RegExpGet(text, pattern50)
         text = RegExpReplace(text, "([A-Z0-9]+) ([A-Z0-9]+) ([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern60)
         text = RegExpGet(text, pattern60)
         text = RegExpReplace(text, "([A-Z0-9]+)-([A-Z0-9]+)-([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern70)
         barcode = RegExpReplace(text, pattern70, "$1")
      Case RegExpTest(text, pattern80)
         text = RegExpGet(text, pattern80)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = text
      Case RegExpTest(text, pattern90)
         text = RegExpGet(text, pattern90)
         text = RegExpReplace(text, "^P(PC[A-Z0-9]+)$", "$1")
         barcode = text
      Case Else
         barcode = "N/A"
   End Select
End Function

Private Function RegExpTest(txt As String, pat As String) As Boolean
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   If rex.test(txt) = True Then
      RegExpTest = True
   Else
      RegExpTest = False
   End If
End Function

Private Function RegExpReplace(txt As String, pat As String, rep As String) As String
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = True
   RegExpReplace = rex.Replace(txt, rep)
End Function

Private Function RegExpGet(txt As String, pat As String)
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = False
   RegExpGet = rex.Execute(txt)(0)
End Function

As you can see I got a 81% success rate

I suggest you test my function with your whole data and check what "success rate" you get.

Then please let me know how we should proceed or how my function should be modified.

@2KGrafix
I just found a mistake:

VBA Code:
Private Function barcode(text As String) As String
Private Function should be Public Function otherwise it will not work in a worksheet.

VBA Code:
Public Function barcode(text As String) As String
 
Upvote 0
@2KGrafix
I updated my functions an created a table to visualize the results:

barcode_scan.xlsm
ABCDE
1MLDLB5B17F775BE5YZ9, 22:57:38 1/26/2024, SEQ 224MLDLB5B17F775BE5YZ9MLDLB5B17F775BE5YZ9TRUE
2MLDLC5B17F954BE5YBT, 11:52: 2 1/22/2024, SEQ 257MLDLC5B17F954BE5YBTMLDLC5B17F954BE5YBTTRUE
3PLB5B 13B414 AB S10155600 D7/6/1998 0:22:1PCLB5B13B414ABPCLB5B13B414ABTRUE
4PPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWPCLB5B17F771BB5UAWTRUE
5LB5B-8200-EB5UAWPCLB5B8200EB5UAWPCLB5B8200EB5UAWTRUE
6PCLC5B-15A227-ACPCLC5B15A227ACPCLC5B15A227ACTRUE
7PLC5B 17A848 AB S10167471 D1/15/2024 8:48:9 12345PCLC5B17A848ABPCLC5B17A848ABTRUE
8PPCLB5B17C831AE5KBXPCLC5B17C831AE5KBXPCLB5B17C831AE5KBXFALSEWrong result! "PCLC" should be "PPCLB"
9PPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMPCLC5B17C831AE5LPMTRUE
10PCLC5B17E911AASMASPCLC5B17E911AASMASPCLC5B17E911AASMASTRUE
11PCLC5B17E911CA59B8 10324PCLC5B17E911CA59B8PCLC5B17E911CA59B8TRUE
12PPCLC5B17F001AH51MDPCLC5B17F001AH51MDPCLC5B17F001AH51MDTRUE
13PPCLC5B17K945AD59B8PCLC5B17K945AD59B8PCLC5B17K945AD59B8TRUE
14PCLC5B8200BESMA4|240238352706||20240123181548PCLC5B8200BESMA4PCLC5B8200BESMA4TRUE
15LC5B-8200-DE5KW9PCLC5B8200DE5KW9PCLC5B8200DE5KW9TRUE
16L1MT-13A803-ABPCL1MT13A803ABPCL1MT13A803ABTRUE
17L1M3-8B273-BAF,EE0WA,24010,18:10:12PCL1M38B273BAFPCL1M38B273BAFTRUE
18L1M38005BF7482PCL1M38005BFN/AFALSEno pattern possible: extra "7482" at the end
19PPCMB5J17F771AA51MDPCMB5J17F771AA51MDPCMB5J17F771AA51MDTRUE
20M1M3-8W005-AAA C757LPCM1M38W005AAAPCM1M38W005AAATRUE
21P1MT-15K867-BBAPCP1MT15K867BBAPCP1MT15K867BBATRUE
22P1M38C607FAPCP1M38C607FAPCP1M38C607FATRUE
23P1MT 14N139 BADBPCP1MT14N139BADBPCP1MT14N139BADBTRUE
24MB5B13W030JG240125045754PCMB5B13W030JGN/AFALSEcurrent patterns don't match the extra length "240125045754" at the end
25P1MT-14A303-BBCAPCP1MT14A303BBCAPCP1MT14A303BBCATRUE
26RB5B-8200-DBSMA4|0001|P3340|YYYYMMDDHHMMSS|PCRB5B8200DBSMA4PCRB5B8200DBSMA4TRUE
27CRC5B8200CBSMAS111623 ~233205437218~FD834.1A00.CA11PCRC5B8200CBSMASN/AFALSEno pattern possible: 1) extra "C" at the beginning; 2) extra "111623" at the end
28RB5B13W020CG RH SAE HIGH U625(2) 12/05/23 16:36:55PCRB5B13W029CGPCRB5B13W020CGFALSEWrong result! number "29" instead of "20"
29PPCRC5B17C831BC53CCPCRC5B17C831BB53CCPCRC5B17C831BC53CCFALSEWrong result! "BB53" should be "BC53" at the end
30RC5B13E014AEPCRC5B13E014AEPCRC5B13E014AETRUE
31PPCRB5B17F765BA5JP4PCRB5B17F765BA5JP4PCRB5B17F765BA5JP4TRUE
32
33total right guesses25
34right guess rate81%
Sheet2
Cell Formulas
RangeFormula
C1:C31C1= barcode(A1)
D1:D31D1=C1=B1
D33D33= COUNTIF(D1:D31,TRUE)
D34D34= D33/COUNTA(D1:D31)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D1:D31Cell Value=FALSEtextNO
D1:D31Cell Value=TRUEtextNO
A1:C31Expression=$D1=FALSEtextNO


Here's the VBA code you should just paste into a (new) module:

VBA Code:
Option Explicit

Private Function barcode(text As String) As String
   Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
   Dim temp As String
   Dim pattern10 As String: pattern10 = "(MLD[A-Z0-9]+),.*"
   Dim pattern20 As String: pattern20 = "(PC[A-Z0-9]+)\|.*"
   Dim pattern30 As String: pattern30 = "^([A-Z0-9]{12}) .*"
   Dim pattern40 As String: pattern40 = "^([A-Z0-9]{10,12})$"
   Dim pattern50 As String: pattern50 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)"
   Dim pattern60 As String: pattern60 = "([A-Z0-9]{4})-([A-Z0-9]+)-([A-Z0-9]+)"
   Dim pattern70 As String: pattern70 = "^(PC[A-Z0-9]+)$"
   Dim pattern80 As String: pattern80 = "^(PC[A-Z0-9]+) [A-Z0-9]+"
   Dim pattern90 As String: pattern90 = "^P(PC[A-Z0-9]+)$"
 
   Select Case True
      Case RegExpTest(text, pattern10)
         text = RegExpReplace(text, pattern10, "$1")
         barcode = text
      Case RegExpTest(text, pattern20)
         text = RegExpReplace(text, pattern20, "$1")
         barcode = text
      Case RegExpTest(text, pattern30)
         text = RegExpGet(text, pattern30)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = "PC" & text
      Case RegExpTest(text, pattern40)
         text = RegExpGet(text, pattern40)
         barcode = "PC" & text
      Case RegExpTest(text, pattern50)
         text = RegExpGet(text, pattern50)
         text = RegExpReplace(text, "([A-Z0-9]+) ([A-Z0-9]+) ([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern60)
         text = RegExpGet(text, pattern60)
         text = RegExpReplace(text, "([A-Z0-9]+)-([A-Z0-9]+)-([A-Z0-9]+)", "$1$2$3")
         barcode = "PC" & text
      Case RegExpTest(text, pattern70)
         barcode = RegExpReplace(text, pattern70, "$1")
      Case RegExpTest(text, pattern80)
         text = RegExpGet(text, pattern80)
         text = RegExpReplace(text, "([A-Z0-9]+) .*", "$1")
         barcode = text
      Case RegExpTest(text, pattern90)
         text = RegExpGet(text, pattern90)
         text = RegExpReplace(text, "^P(PC[A-Z0-9]+)$", "$1")
         barcode = text
      Case Else
         barcode = "N/A"
   End Select
End Function

Private Function RegExpTest(txt As String, pat As String) As Boolean
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   If rex.test(txt) = True Then
      RegExpTest = True
   Else
      RegExpTest = False
   End If
End Function

Private Function RegExpReplace(txt As String, pat As String, rep As String) As String
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = True
   RegExpReplace = rex.Replace(txt, rep)
End Function

Private Function RegExpGet(txt As String, pat As String)
   Dim rex As Object: Set rex = CreateObject("VBScript.RegExp")
   rex.Pattern = pat
   rex.Global = False
   RegExpGet = rex.Execute(txt)(0)
End Function

As you can see I got a 81% success rate

I suggest you test my function with your whole data and check what "success rate" you get.

Then please let me know how we should proceed or how my function should be modified.
This is great. I only have one request. Could you combine this set of patterns and code with the first one you sent me? I noticed one of the barcodes in the first version that worked no longer worked in the last version of code.
 
Upvote 0
This is great. I only have one request. Could you combine this set of patterns and code with the first one you sent me? I noticed one of the barcodes in the first version that worked no longer worked in the last version of code.
You are right, some codes are not working anymore that's because I changed the order in which checks are performed.

VBA Code:
   Dim pattern10 As String: pattern10 = "(MLD[A-Z0-9]+),.*"
   Dim pattern20 As String: pattern20 = "(PC[A-Z0-9]+)\|.*"
   Dim pattern30 As String: pattern30 = "^([A-Z0-9]{12}) .*"
   Dim pattern40 As String: pattern40 = "^([A-Z0-9]{10,12})$"
   Dim pattern50 As String: pattern50 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)"
   Dim pattern60 As String: pattern60 = "([A-Z0-9]{4})-([A-Z0-9]+)-([A-Z0-9]+)"
   Dim pattern70 As String: pattern70 = "^(PC[A-Z0-9]+)$"
   Dim pattern80 As String: pattern80 = "^(PC[A-Z0-9]+) [A-Z0-9]+"
   Dim pattern90 As String: pattern90 = "^P(PC[A-Z0-9]+)$"

What you see is a sequence of checks against the text string to find a match.
(I made steps of 10 in numbering to make it easier to insert new patterns without the need to renumber everything).
The first pattern that results in a match will be taken as right, disregarding all following patterns. So order is very important here.
I wrote a very simple and "stupid" function which does not take into account that another match would fit better.

But maybe together we can figure something out that is a little bit smarter.
For that purpose I need your input.

First, is my attempt good enough to work with? Or would you suggest something different than that?
Remember, code is stupid, it follows rules. Can you find some rules that work better than mine?

Please let me know what you think.

Best regards
 
Upvote 0
You are right, some codes are not working anymore that's because I changed the order in which checks are performed.

VBA Code:
   Dim pattern10 As String: pattern10 = "(MLD[A-Z0-9]+),.*"
   Dim pattern20 As String: pattern20 = "(PC[A-Z0-9]+)\|.*"
   Dim pattern30 As String: pattern30 = "^([A-Z0-9]{12}) .*"
   Dim pattern40 As String: pattern40 = "^([A-Z0-9]{10,12})$"
   Dim pattern50 As String: pattern50 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)"
   Dim pattern60 As String: pattern60 = "([A-Z0-9]{4})-([A-Z0-9]+)-([A-Z0-9]+)"
   Dim pattern70 As String: pattern70 = "^(PC[A-Z0-9]+)$"
   Dim pattern80 As String: pattern80 = "^(PC[A-Z0-9]+) [A-Z0-9]+"
   Dim pattern90 As String: pattern90 = "^P(PC[A-Z0-9]+)$"

What you see is a sequence of checks against the text string to find a match.
(I made steps of 10 in numbering to make it easier to insert new patterns without the need to renumber everything).
The first pattern that results in a match will be taken as right, disregarding all following patterns. So order is very important here.
I wrote a very simple and "stupid" function which does not take into account that another match would fit better.

But maybe together we can figure something out that is a little bit smarter.
For that purpose I need your input.

First, is my attempt good enough to work with? Or would you suggest something different than that?
Remember, code is stupid, it follows rules. Can you find some rules that work better than mine?

Please let me know what you think.

Best regards
Let me play around with the order. There may be some components that we won't need to scan at all, eliminating some patterns in favor of others.
 
Upvote 0
Let me play around with the order. There may be some components that we won't need to scan at all, eliminating some patterns in favor of others.
Okay.
Do you need any assistance?

Let me explain Regular Expressions a little...

Capturing:
[A-Z0-9] will find any capital letter or digit
. will find any character
^ will find the beginning of a string
$ will find the end of a string

Repetition:
* will find zero or more occurrences of previous item
+ will find one or more occurrences of previous item
? will find zero or one occurrences of previous item
{n} will find exactly n occurrences of previous item
{a,b} will find at least a but not more than b occurrences of previous item

Grouping:
(...) will group the items between brackets into one capturing group
$n return the items of the n-th capturing group

Special characters:
Special characters are part of the Regular Expression Syntax and can not be entered for searching.
Instead they should be "escaped" by the "escape character" which is a backslash "\"
\| will match a |
\$ will match $ (dollar)
\. will match . (period)
and so on...


Maybe this helps you a little
 
Upvote 0

Forum statistics

Threads
1,224,752
Messages
6,180,742
Members
452,996
Latest member
nelsonsix66

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