How to print the sorted array in word using VBA?

柔情痞子 提交于 2020-01-25 00:59:07

问题


I have a two Input data of Year and Name in separate two arrays. I need to sort both the array values first i need to sort it chronologically(Year) and then if year information repeats it will sort the Array Alphabetically.

As for as I complete the sorting for both year and then name. Using Wordbasic.sortarray command

Input: (Before sorting)

SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986

Output: (After sorting)

PJK 1983
EFD 1986
ELS 1986
WKL 1995
SDF 1997

If I print it in word it printed like this:

PJK 1983, ELS 1986, EFD 1986, WKL 1995, SDF 1997.

Here is my code for Printing the data. Would anyone please look into this and guide me where did i made mistake?

WordBasic.sortarray SortyearArray()

Code:

Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim N As Integer
Dim Counter As Integer
COUNTER1 = 1

i1 = 1
J1 = 5

For I = 0 To UBound(SortyearArray())
    Counter = 1
    For J = I + 1 To UBound(SortyearArray())
        If SortyearArray(I) = SortyearArray(J) Then
            Counter = Counter + 1
            MsgBox (Counter)
        End If
        COUNTER1 = Counter + COUNTER1
    Next J
    If Counter = 1 Then
        For N = i1 To J1
            If SortyearArray(I) = Year(N) Then
                Selection.TypeText Text:="(" & AuthorName(N) & Year(N) & ")"
            End If
        Next N
    End If
Next I

回答1:


The input

SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986

Core functions:

    Public Function QuickSort(ByRef array2check() As String, min As Long, max As Long) As Boolean
Dim lo As Long, hi As Long
Dim lo0 As Long, hi0 As Long
Dim midPos As String

    lo = min: hi = max
    lo0 = lo: hi0 = hi
    midPos = array2check((lo0 + hi0) / 2)
    DoEvents
    While (lo <= hi)
        While ((lo < hi0) And (array2check(lo) < midPos))
            lo = lo + 1
        Wend
        While ((hi > lo0) And (array2check(hi) > midPos))
            hi = hi - 1
        Wend
        If lo <= hi Then
            Call swap(array2check, lo, hi)
            lo = lo + 1
            hi = hi - 1
        End If
    DoEvents
    Wend

    If lo0 < hi Then Call QuickSort(array2check, lo0, hi)
    If lo < hi0 Then Call QuickSort(array2check, lo, hi0)

    QuickSort = True

End Function

Private Sub swap(arr() As String, idx1 As Long, idx2 As Long)
Dim tmp As String
    tmp = arr(idx1)
    arr(idx1) = arr(idx2)
    arr(idx2) = tmp
End Sub

The Sample tester

Public Sub sample_test()
    Dim test_arr() As String
    test_arr = Split("SDF 1997" & vbCrLf & "ELS 1986" & vbCrLf & "PJK 1983" & vbCrLf & "WKL 1995" & vbCrLf & "EFD 1986", vbCrLf)
    If QuickSort(test_arr, LBound(test_arr), UBound(test_arr)) = True Then
        'Debug.Print Join(test_arr, vbCrLf)
        MsgBox Join(test_arr, vbCrLf)
    End If
End Sub

The Result

EFD 1986
ELS 1986
PJK 1983
SDF 1997
WKL 1995

Hope this helps.



来源:https://stackoverflow.com/questions/11011408/how-to-print-the-sorted-array-in-word-using-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!