Sequential Checker Pattern


Hi,
I want to seek for help of macro experts there.
I have here a macro code that needs to be labeled for me to understand it.
Sub test()
    Dim a, i As Long, temp, x, result
    a = Range("a1").CurrentRegion.Value
    ReDim Preserve a(1 To UBound(a, 1), 1 To 5)
    For i = 1 To UBound(a, 1)
        temp = CheckPattern(a(i, 1))
        x = Split(temp, "|")
        If UBound(x) = 1 Then
            a(i, 2) = x(0)
            a(i, 3) = x(1)
            a(i, 5) = a(i, 1)
        Else
            a(i, 2) = x(0)
            a(i, 3) = x(1)
            a(i, 4) = x(2)
            a(i, 5) = x(0) & x(1)
        End If
    Next
    x = GetGrouped(a)
    x = GetSeries(x(0), x(1), 100)
    result = GetAligned(a, x)
    Application.ScreenUpdating = False
    For i = 1 To UBound(result, 1)
        If result(i, 1) = "" Then Rows(i).Insert
    Next
    Range("a1").Resize(UBound(result, 1), 2).Value = result
    Application.ScreenUpdating = True
End Sub
 
 
Private Function CheckPattern(ByVal txt As String) As String
    Dim Fnum, Lnum
    With CreateObject("VBScript.RegExp")
        .Pattern = "^(\D+)(\d+)$"
        .IgnoreCase = True
        If .test(txt) Then
            CheckPattern = .Replace(txt, "$1|$2")
        Else
            .Pattern = "^(\D+)(\d+) ?\-(.*\D)?(\d+)$"
            If .test(txt) Then
                Fnum = .Replace(txt, "$2")
                Lnum = .Replace(txt, "$4")
                If Len(Fnum) <> Len(Lnum) Then
                    Lnum = Application.Replace(Fnum _
                    , Len(Fnum) - Len(Lnum) + 1, Len(Fnum), Lnum)
                End If
                CheckPattern = .Replace(txt, "$1|") & Fnum & "|" & Lnum
            Else
                .Pattern = "^(\D+)(\d+) DEN (\d+) .*$"
                If .test(txt) Then
                    Fnum = .Replace(txt, "$2")
                    Lnum = .Replace(txt, "$3")
                    If Len(Fnum) <> Len(Lnum) Then
                        Lnum = Application.Replace(Fnum _
                        , Len(Fnum) - Len(Lnum) + 1, Len(Fnum), Lnum)
                    End If
                    CheckPattern = .Replace(txt, "$1|") & Fnum & "|" & Lnum
                End If
            End If
        End If
    End With
End Function
 
 
 
 
Private Function GetGrouped(a As Variant) As Variant
    Dim i As Long, w(), myNum
    With CreateObject("Scripting.Dictionary")
        .comparemode = 1
        For i = 1 To UBound(a, 1)
            If Not .exists(a(i, 2)) Then
                ReDim w(1 To 4, 1 To 1)
                w(1, 1) = a(i, 3)
                w(2, 1) = IIf(a(i, 4) = "", a(i, 3), a(i, 4))
                w(3, 1) = a(i, 3)
                .Item(a(i, 2)) = w
            Else
                w = .Item(a(i, 2))
                ReDim Preserve w(1 To 4, 1 To UBound(w, 2) + 1)
                w(1, UBound(w, 2)) = a(i, 3)
                w(2, UBound(w, 2)) = IIf(a(i, 4) = "", a(i, 3), a(i, 4))
                w(3, UBound(w, 2)) = _
                w(2, UBound(w, 2)) - Val(w(1, UBound(w, 2) - 1))
                .Item(a(i, 2)) = w
            End If
        Next
        GetGrouped = VBA.Array(.keys, .items)
    End With
End Function
 
 
Function GetSeries(x, y, myLimit)
    Dim i As Long, ii As Long, iii As Long
    With CreateObject("System.Collections.ArrayList")
        For i = LBound(x) To UBound(x)
            If UBound(y(i), 2) = 1 Then
                For iii = y(i)(1, 1) To y(i)(2, 1)
                    .Add x(i) & iii
                Next
            Else
                .Add x(i) & y(i)(1, 1)
                For ii = 2 To UBound(y(i), 2)
                    If y(i)(2, ii) > y(i)(2, ii - 1) And y(i)(3, ii) < myLimit Then
                        For iii = y(i)(2, ii - 1) + 1 To y(i)(2, ii)
                            .Add x(i) & iii
                        Next
                    Else
                        .Add x(i) & y(i)(1, ii)
                    End If
                Next
            End If
        Next
        GetSeries = .ToArray
    End With
End Function
 
 
Function GetAligned(p, x)
    Dim i As Long, temp
    ReDim a(1 To UBound(x) + 1, 1 To 2)
    With CreateObject("Scripting.Dictionary")
        For i = 0 To UBound(x)
            .Item(x(i)) = i + 1
            a(i + 1, 2) = x(i)
        Next
        For i = 1 To UBound(p, 1)
            temp = p(i, 2) & p(i, 3)
            If .exists(temp) Then a(.Item(temp), 1) = p(i, 1)
        Next
        GetAligned = a
    End With
End Function
 
The function of these codes is to check and make modifications on a number of sequence.
Lets say these are my data
Cell A1 ES100000
Cell A2 ES100001
Cell A3 ES100002
Cell A4 ES100006
Cell A5 D1000001-D1000005
Cell A6 D1000008
Cell A7 M2000009-20
Cell A8 F1000000
 

In Cell A1-Cell A4, it is a sequence order, ES100000 then ES100001 then ES100002 then it will insert rows for ES100003 to ES100005 then ES100006. The sequence is in 100 rule that if the suceeding cell is within +100 of the cell above it, then it is still part of a sequence. So its whole sequence will be ES100000 up to ES100006 that will occupy Cell A1-Cell A7 respectively. Then the sequence D1000001-D1000005, it will seperate the values as D1000001, D1000002, and so on until D1000005 then add again D1000006 and D1000007 since the succeeding value is D1000008 which is still within the +100 rule range. It is also the same for M2000009 up to M2000020.
 
Now, the help I need is:
1. To label the codes for me to be able to understand how each code works
2. To mark those added ranges that is really not part of the given sequence (e.g in D1000001-D1000008, added value is D1000006 and D1000007)
3. To be able to identify those values that has no sequence (lets say F1000000, there are no other values that would supprt its sequence.
 
Thanks you in advance for your help!
 
Marvs

 
Vishesh's picture

I have run this code on my

I have run this code on my system and codes D1000001-D1000005 are not getting created properly. Could you please recheck if this is how it is supposed to work.

Code working

Hi Vishesh,
Have tried to paste these codes in a new workbook (no changes in any part), it does work with the given data.
I'll try to attach a file later.
Marvs

Code Not Working

Hi again Vishesh,
Now I understand. Have overlooked it and found that it doesn't work on D1000001-D1000005. But in ES10001-ES10004, it works. I don't know why. Can you help me with this?
Thanks!

Vishesh's picture

Request a Quote

Hi,

This is going to take a bit more of effort please goto http://excelexperts.com/contact

How does contact form works?

Hi,
Regret but how does this work?
And cannot attached a file for my post.
=(

Nick's picture

to attach a file to the

to attach a file to the post:
1. log in
2. edit the post
3. Choose File attachments