Hallo,
mit RegExpressions geht es relativ einfach:
Function Encrypt(value As String) As String
Dim vval() As String
Dim strOut As String
Dim iVal As Integer
Dim iCnt As Integer, iStart As Integer, iEnd As Integer, iStep As Integer
Dim mc As VBScript_RegExp_55.MatchCollection
vval = Split(value, ",")
For iVal = 0 To UBound(vval)
If InStr(vval(iVal), "-") > 0 Then
' Reihenfolge generieren
iStep = 1
Set mc = match(vval(iVal), ":([0-9]+)")
If Not mc Is Nothing Then
iStep = mc.Item(0).SubMatches(0)
End If
Set mc = match(vval(iVal), "([0-9]+)-([0-9]+)")
If Not mc Is Nothing Then
iStart = mc.Item(0).SubMatches(0)
iEnd = mc.Item(0).SubMatches(1)
End If
For iCnt = iStart To iEnd Step iStep
strOut = strOut & IIf(strOut = "", "", ";") & CStr(iCnt)
Next
Else
strOut = strOut & IIf(strOut = "", "", ";") & vval(iVal)
End If
Next
Encrypt = strOut
End Function
Function match(value As String, strPattern As String) As VBScript_RegExp_55.MatchCollection
Dim regEx As New VBScript_RegExp_55.RegExp
regEx.Global = True
regEx.pattern = strPattern
regEx.IgnoreCase = True
If regEx.Test(value) Then
Set match = regEx.Execute(value)
Else
Set match = Nothing
End If
End Function
Im VBA Editor muss ein Verweis auf die Bibliothek "Microsoft VBScript Regular Expressions 5.5" gesetzt werden.
Aufruf mit:
Sub Test()
Dim Vorgabe As String
Vorgabe = "1,3,7-18:2,20-24,30"
Debug.Print Encrypt(Vorgabe)
End Sub
LG, BigBen
|