2011-04-28 203 views
1

這裏是我有的代碼,按升序排列數組中的單詞。我需要幫助來改變它,以便它還將以單一功能降序排列單詞。請幫忙。謝謝!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 

回答

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)

+0

感謝功能得到這個一個工作。感謝那裏的朋友! – decrementor 2011-04-30 07:33:36

相關問題