• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sort data In multi pages form

mohadin

Active Member
Hi All;
I have in the attached below
mult_ pages filled with data
what I entend to do is to sort all the pages according to column "c"
the order requiered is in data sheet columns H
"I included the expected result in the attachment"
any idea how to do such a sorting
please
Thanks
 

Attachments

  • SORT data_chandoo.xlsx
    76.1 KB · Views: 12
Code:
Sub test()
    Dim myArea As Range, a, b, i As Long, ii As Long
    Dim n As Long, ub As Long, x, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets("data").[h1].CurrentRegion.Value
    For i = 1 To UBound(a, 1)
        dic(a(i, 1)) = Format$(a(i, 2), String(12, "0"))
    Next
    With Sheets("main")
        ub = .Columns(1).SpecialCells(2, 1).Areas(1).CurrentRegion.Columns.Count - 1
        ReDim a(1 To .Columns(1).SpecialCells(2, 1).Count, 1 To ub + 1)
        For Each myArea In .Columns(1).SpecialCells(2, 1).Areas
            With myArea.Offset(, 1).Resize(, ub + 1)
                b = .Value
                For i = 1 To UBound(b, 1)
                    n = n + 1
                    For ii = 1 To ub
                        a(n, ii) = b(i, ii)
                    Next
                    x = GetSortVal(a(n, 1))
                    If dic.exists(a(n, 2)) Then
                        a(n, ub + 1) = dic(a(n, 2)) & " " & x
                    Else
                        a(n, 7) = "zzz " & x
                    End If
                Next
            End With
        Next
        VSortM a, 1, n, 7
        x = 1
        For Each myArea In .Columns(1).SpecialCells(2, 1).Areas
            n = myArea.Count
            myArea.Offset(, 1).Resize(n, ub).Value = Application.Index(a, Evaluate("row(" & x & ":" & _
            x + n - 1 & ")"), Evaluate("column(" & [a1].Resize(, ub).Address & ")"))
            x = x + n
        Next
    End With
End Sub

Function GetSortVal(ByVal txt As String) As String
    Dim i As Long, m As Object
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\d+"
        If .test(txt) Then
            For i = .Execute(txt).Count - 1 To 0 Step -1
                Set m = .Execute(txt)(i)
                txt = Application.Replace(txt, m.firstindex + 1, m.Length, Format$(m.Value, String(12, "0")))
            Next
        End If
    End With
    GetSortVal = txt
End Function

Private Sub VSortM(ary, LB, ub, ref)
    Dim i As Long, ii As Long, iii As Long, m, temp
    i = ub: ii = LB
    m = ary(Int((LB + ub) / 2), ref)
    Do While ii <= i
        Do While ary(ii, ref) < m: ii = ii + 1: Loop
        Do While ary(i, ref) > m: i = i - 1: Loop
        If ii <= i Then
            For iii = LBound(ary, 2) To UBound(ary, 2)
                temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
            Next
            i = i - 1: ii = ii + 1
        End If
    Loop
    If LB < i Then VSortM ary, LB, i, ref
    If ii < ub Then VSortM ary, ii, ub, ref
End Sub
 

Attachments

  • SORT data_chandoo with code.xlsm
    89.8 KB · Views: 16
You are amazing Mr. Jindon
I thought it was very complicated issue but you make it solved in perfect way

Just a little remark:
I don't know what the OP think about it ...The formulas existed turned to values
I think he may need to keep formulas and do the sort process

Regards
 
Incredible .. you are incredible and have unique mind
Thanks a lot for this little and useful piece of information
 
Code:
Sub test()
    Dim myArea As Range, a, b, i As Long, ii As Long
    Dim n As Long, ub As Long, x, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets("data").[h1].CurrentRegion.Value
    For i = 1 To UBound(a, 1)
        dic(a(i, 1)) = Format$(a(i, 2), String(12, "0"))
    Next
    With Sheets("main")
        ub = .Columns(1).SpecialCells(2, 1).Areas(1).CurrentRegion.Columns.Count - 1
        ReDim a(1 To .Columns(1).SpecialCells(2, 1).Count, 1 To ub + 1)
        For Each myArea In .Columns(1).SpecialCells(2, 1).Areas
            With myArea.Offset(, 1).Resize(, ub + 1)
                b = .Value
                For i = 1 To UBound(b, 1)
                    n = n + 1
                    For ii = 1 To ub
                        a(n, ii) = b(i, ii)
                    Next
                    x = GetSortVal(a(n, 1))
                    If dic.exists(a(n, 2)) Then
                        a(n, ub + 1) = dic(a(n, 2)) & " " & x
                    Else
                        a(n, 7) = "zzz " & x
                    End If
                Next
            End With
        Next
        VSortM a, 1, n, 7
        x = 1
        For Each myArea In .Columns(1).SpecialCells(2, 1).Areas
            n = myArea.Count
            myArea.Offset(, 1).Resize(n, ub).Value = Application.Index(a, Evaluate("row(" & x & ":" & _
            x + n - 1 & ")"), Evaluate("column(" & [a1].Resize(, ub).Address & ")"))
            x = x + n
        Next
    End With
End Sub

Function GetSortVal(ByVal txt As String) As String
    Dim i As Long, m As Object
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\d+"
        If .test(txt) Then
            For i = .Execute(txt).Count - 1 To 0 Step -1
                Set m = .Execute(txt)(i)
                txt = Application.Replace(txt, m.firstindex + 1, m.Length, Format$(m.Value, String(12, "0")))
            Next
        End If
    End With
    GetSortVal = txt
End Function

Private Sub VSortM(ary, LB, ub, ref)
    Dim i As Long, ii As Long, iii As Long, m, temp
    i = ub: ii = LB
    m = ary(Int((LB + ub) / 2), ref)
    Do While ii <= i
        Do While ary(ii, ref) < m: ii = ii + 1: Loop
        Do While ary(i, ref) > m: i = i - 1: Loop
        If ii <= i Then
            For iii = LBound(ary, 2) To UBound(ary, 2)
                temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
            Next
            i = i - 1: ii = ii + 1
        End If
    Loop
    If LB < i Then VSortM ary, LB, i, ref
    If ii < ub Then VSortM ary, ii, ub, ref
End Sub


Hi MR. jindon
first of all I'd like to it "mega like"
What a master Piece you put in
Thank you very much.
I need some time to understand the code
So I I will need your help understand it, if you do not mind?
Thank you again and again
Best regards
 
1) The key is Function GetSortVal.
This enables sort the data properly for alphanumeric data.
2) To sort entire data, it needs to be 1 array to store all data.
3) Distribute sorted data from the array to the block of cells.
4) The block is determined numeric data in col.A

Any question?
 
Hi

to be honest with you
I have many many question
you made me feel as I know nothing in excel
I need to go to your school
what is your school address

Bless you
 
Back
Top