如何使用VBA在单词中打印排序的数组? [英] How to print the sorted array in word using VBA?
问题描述
我在单独的两个数组中有Year和Name的两个输入数据.首先,我需要对两个数组值进行排序,我需要按时间顺序(年份)对其进行排序,然后,如果年份信息重复,则将按字母顺序对数组进行排序.
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.
至于我完成了对年份和名称的排序.使用Wordbasic.sortarray
命令
As for as I complete the sorting for both year and then name. Using Wordbasic.sortarray
command
输入 :(排序前)
SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986
输出 :(排序后)
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()
代码:
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
推荐答案
输入
SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986
核心功能:
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
样本测试器
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
结果
EFD 1986
ELS 1986
PJK 1983
SDF 1997
WKL 1995
希望这会有所帮助.
Hope this helps.
这篇关于如何使用VBA在单词中打印排序的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!