Share via

Sorting two-dimensional array in a Word macro

Anonymous
2024-02-18T05:37:05+00:00

Hi Dear Buddies:

I need to sort a two-dimensional array in a Word macro.

Column 1 of the array is a number (page number) and in column 2 is a string of text.

Sometimes there are only a few rows in the array. Sometimes there are many. I Googled around and it seems that there is no simple function for sorting arrays in Word VBA. Am I missing something? And what is the easiest way to actually do this?

Thank you and have a good day!

Susan Flamingo

Microsoft 365 and Office | Word | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2024-02-18T14:08:01+00:00

Does this work better?

Sub MySort(arr)
    Dim i As Long
    Dim j As Long
    Dim tmp1 As Long
    Dim tmp2 As String
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If Val(arr(i, 1)) > Val(arr(j, 1)) Then
                tmp1 = arr(i, 1)
                arr(i, 1) = arr(j, 1)
                arr(j, 1) = tmp1
                tmp2 = arr(i, 2)
                arr(i, 2) = arr(j, 2)
                arr(j, 2) = tmp2
            End If
        Next j
    Next i
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2024-02-18T12:43:31+00:00

    Here is a simpler approach.

    Copy the following code into a module:

    Sub MySort(arr)
        Dim i As Long
        Dim j As Long
        Dim tmp1 As Long
        Dim tmp2 As String
        For i = LBound(arr) To UBound(arr) - 1
            For j = i + 1 To UBound(arr)
                If arr(i, 1) > arr(j, 1) Then
                    tmp1 = arr(i, 1)
                    arr(i, 1) = arr(j, 1)
                    arr(j, 1) = tmp1
                    tmp2 = arr(i, 2)
                    arr(i, 2) = arr(j, 2)
                    arr(j, 2) = tmp2
                End If
            Next j
        Next i
    End Sub
    

    To sort your array arrEntries on the first column, use

    MySort arrEntries
    

    Was this answer helpful?

    0 comments No comments
  2. Suzanne S Barnhill 278.1K Reputation points MVP Volunteer Moderator
    2024-02-18T12:20:04+00:00

    It appears that the sort is set to text rather than number. I'm VBA-less, so I can't help beyond noting that.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-02-18T09:07:28+00:00

    Thank you for the reference. I didn't think is was such a big deal.

    I am struggling to tweak a macro that someone else has written and the problematic line is

    >>> WordBasic.SortArray arrEntries(), 0, 0, UBound(arrEntries), 0, 1

    It sorts the page numbers in the first column of the array, but whenever the page number is one digit long it puts it at the end 10, 11,.....2. How can I tell it that 2 comes before 10 (even though I failed math in high school I know that:)

    Thank you and have a good day!

    Susan Flamingo

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2024-02-18T08:51:08+00:00

    Was this answer helpful?

    0 comments No comments