Merge Sort in VBA in MS Project

brodaddy02

New Member
Joined
Mar 2, 2012
Messages
7
I can't seem to get the merge sort working. I think it has something to do with CopyMemory function call. I am trying to use it in MS Project. Code works on a smaller test data, but my 27000 data array the code doesn't work. Code is taken from
HTML:
http://www.vb-helper.com/howto_mergesort.html

Code:
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias _    "RtlMoveMemory" (destination As Date, source As Date, _
    ByVal length As Long)

Public Sub MergeSort(ByRef keyArray() As String, ByRef list() As Date, ByVal first_index As Long, ByVal last_index As Long)
    Dim middle As Long


    If (last_index > first_index) Then
        ' Recursively sort the two halves of the list.
        middle = (first_index + last_index) \ 2
        MergeSort keyArray, list, first_index, middle
        MergeSort keyArray, list, middle + 1, last_index


        ' Merge the results.
        Merge keyArray, list, first_index, middle, last_index
    End If
End Sub


' Merge two sorted sublists.
Public Sub Merge(ByRef keyArray() As String, ByRef list() As Date, ByVal beginning As Long, ByVal middle As Long, ByVal ending As Long)
    Dim temp_array() As Date  'holds temporary list data
    Dim temp_Key_Array() As String
    Dim temp As Long
    Dim counterA As Long
    Dim counterB As Long
    Dim counterMain As Long
    Dim i As Long


        ' Copy the array into a temporary array.
        ReDim temp_array(beginning To ending)
        ReDim temp_Key_Array(beginning To ending)
        CopyMemory temp_array(beginning), list(beginning), _
            (ending - beginning + 1) * Len(list(beginning))


        For i = beginning To ending
            temp_Key_Array(i) = keyArray(i)
        Next i

        ' counterA and counterB mark the next item to save
        ' in the first and second halves of the list.
        counterA = beginning
        counterB = middle + 1


        ' counterMain is the index where we will put the
        ' next item in the merged list.
        counterMain = beginning
        Do While (counterA <= middle) And (counterB <= ending)
            ' Find the smaller of the two items at the front
            ' of the two sublists.
            If (temp_array(counterA) <= temp_array(counterB)) _
                Then
                ' The smaller value is in the first half.
                list(counterMain) = temp_array(counterA)
                keyArray(counterMain) = temp_Key_Array(counterA)
                counterA = counterA + 1
            Else
                ' The smaller value is in the second half.
                list(counterMain) = temp_array(counterB)
                keyArray(counterMain) = temp_Key_Array(counterB)
                counterB = counterB + 1
            End If
            counterMain = counterMain + 1
        Loop


        ' Copy any remaining items from the first half.
        If counterA <= middle Then
            CopyMemory list(counterMain), temp_array(counterA), _
                (middle - counterA + 1) * Len(list(beginning))
        End If


        ' Copy any remaining items from the second half.
        If counterB <= ending Then
            CopyMemory list(counterMain), temp_array(counterB), _
                (ending - counterB + 1) * Len(list(beginning))
        End If


End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,178
Latest member
Emilou

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