Public Function barcode(text As String) As String
Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
Dim temp As String
Dim pattern00 As String: pattern00 = "(MLD[A-Z0-9]+)"
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 pattern44 As String: pattern44 = "^([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)$"
Dim pattern45 As String: pattern45 = "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9_]{2}?)"
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]+)$"
Dim pattern100 As String: pattern100 = "([A-Z0-9]{4})-([A-Z0-9]+)- ([A-Z0-9]+)"
Dim pattern110 As String: pattern110 = "^P(PC[A-Z0-9]+) [A-Z0-9]+$"
Dim pattern120 As String: pattern120 = "^C([A-Z0-9]+) ~[0-9]+~.*$"
Dim pattern130 As String: pattern130 = "^([A-Z0-9]+?[A-Z]{2}?)[A-Z0-9]+$"
Dim pattern140 As String: pattern140 = "^([A-Z0-9]+?[A-Z]+)[0-9]+$"
Select Case True
Case RegExpTest(text, pattern00)
text = RegExpReplace(text, pattern00, "$1")
barcode = text
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, pattern44)
text = RegExpGet(text, pattern44)
text = RegExpReplace(text, "^([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9]+)$", "$1$2$3")
barcode = "PC" & text
Case RegExpTest(text, pattern45)
text = RegExpGet(text, pattern45)
text = RegExpReplace(text, "([A-Z0-9]{4}) ([A-Z0-9]+) ([A-Z0-9_]{2}?)", "$1$2$3")
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 RegExpTest(text, pattern100)
text = RegExpGet(text, pattern100)
text = RegExpReplace(text, "([A-Z0-9]+)-([A-Z0-9]+)- ([A-Z0-9]+)", "$1$2$3")
barcode = "PC" & text
Case RegExpTest(text, pattern110)
text = RegExpGet(text, pattern110)
text = RegExpReplace(text, "^P(PC[A-Z0-9]+) [A-Z0-9]+$", "$1")
barcode = text
Case RegExpTest(text, pattern120)
text = RegExpGet(text, pattern120)
text = RegExpReplace(text, "^C([A-Z0-9]+[^0-9])[0-9]* ~[0-9]+~.*$", "$1")
barcode = "PC" & text
Case RegExpTest(text, pattern130)
text = RegExpGet(text, pattern130)
text = RegExpReplace(text, "^([A-Z0-9]+?[A-Z]{2}?)[A-Z0-9]+$", "$1")
barcode = "PC" & text
Case RegExpTest(text, pattern140)
text = RegExpGet(text, pattern140)
text = RegExpReplace(text, "^([A-Z0-9]+?[A-Z]+)[0-9]+$", "$1")
barcode = "PC" & 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