WORD VBA 升序排序和降序排序

发布于 2024-11-03 19:06:20 字数 902 浏览 3 评论 0原文

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

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

Here is code I have that sorts the words inside an array into ascending order. I need help to change it in such a way that it will also sort the words in descending order, all in a single function. Please help. Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-11-10 19:06:20

如何

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) 调用该函数

How about

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

and call the function using Sort strArray, False '(False Ascending, True Descending)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文