WORD VBA按升序排序和降序排序

时间:2011-04-28 07:52:15

标签: sorting vba word-vba

这是我的代码,它将数组中的单词按升序排序。我需要帮助来改变它,以便它也可以在一个函数中按降序对单词进行排序。请帮忙。谢谢!

Function Sort_Asc(ByRef str() As String)
     Dim iLower As Integer, iUpper As Integer, iCount As Integer, Temp As String
     Dim str2 As String
           iUpper = UBound(str)
           iLower = 1
           Dim bSorted As Boolean
           bSorted = False
           Do While Not bSorted
                bSorted = True
                For iCount = iLower To iUpper - 1
                str2 = StrComp(str(iCount), str(iCount + 1), vbTextCompare)
                     If str2 = 1 Then
                           Temp = str(iCount + 1)
                           str(iCount + 1) = str(iCount)
                           str(iCount) = Temp
                           bSorted = False
                     End If
                Next iCount
             iUpper = iUpper - 1
           Loop
End Function

1 个答案:

答案 0 :(得分:0)

怎么样

Function Sort(ByRef str() As String, ByVal booAsc As Boolean)
 Dim iLower As Integer, iUpper As Integer, iCount As Integer, Temp As String
 Dim str2 As String

       iUpper = UBound(str)
       iLower = 1

       Dim bSorted As Boolean
       bSorted = False
       Do While Not bSorted
            bSorted = True
            For iCount = iLower To iUpper - 1
            If booAsc Then
                str2 = StrComp(str(iCount + 1), str(iCount), vbTextCompare)
            Else
                str2 = StrComp(str(iCount), str(iCount + 1), vbTextCompare)
            End If
                 If str2 = 1 Then
                       Temp = str(iCount + 1)
                       str(iCount + 1) = str(iCount)
                       str(iCount) = Temp
                       bSorted = False
                 End If
            Next iCount
         iUpper = iUpper - 1
       Loop
End Function

并使用Sort strArray, False '(False Ascending, True Descending)

调用该函数