2011-03-30 45 views
0

我有一個excel表單,其中有一個已排序值ex.numbers,名稱的列。 我想驗證使用Vb腳本的值是否排序,而不是手動檢查。那麼在vbscript中是否有任何代碼或方法或函數來驗證上述情況。驗證excel中的列值是否排序

請幫我..... 感謝提前:)

回答

2

你可以通過所有的值進行迭代,並檢查他們的排序:

Dim i as Integer 
i = 2 
Dim oldValue as Integer 
oldValue = MySheet.Cells(1, 1).Value 
Dim sorted as Boolean 
sorted = True 
Do While Not IsEmpty(MySheet.Cells(i, 1)) And sorted 
    If MySheet.Cells(i, 1).Value < oldValue Then 
    sorted = False 
    EndIf 
    oldValue = MySheet.Cells(i, 1).Value 
    i = i + 1 
Loop 
1

你可以使用評估複製公式它會檢查每個值與它下面的值。您必須爲您想要檢查的範圍之外的單元格建立檢查。這隻會檢查提供的範圍的第一列。

Function IsRangeSorted(rRng As Range) As Boolean 

    Dim lResult As Long 

    'If the cell just beyond the range is sorted, evaluate will return zero 
    'If not, evaluate returns 1 but we don't care if that cell is sorted 
    If rRng.Cells(rRng.Rows.Count + 1, 1).Value >= rRng.Cells(rRng.Rows.Count, 1).Value Then 
     lResult = 0 
    Else 
     lResult = 1 
    End If 

    IsRangeSorted = Evaluate("=SUMPRODUCT(--(" & rRng.Columns(1).Address & ">=" & rRng.Columns(1).Offset(1).Address & "))") = lResult 

End Function