从数组中查找具有相同数字的不同数字

时间:2015-05-27 20:10:14

标签: excel vba excel-vba

我有一个6位数的数组,其中我需要找到具有相同数字但顺序不同的数字。我怎么能在vba中做到这一点?

1 个答案:

答案 0 :(得分:2)

我觉得这样做的一个好方法是取你的6位数,创建一个包含这6位数的数组,对它们进行排序,然后给你新的数字(在这个例子中作为字符串返回)然后比较两个确保他们是平等的。

Public Function SortNumber(intIn As Long)
    Dim intArr(1 To 6) As Integer, strResult As String
    Dim i As Integer

    For i = 1 To 6
        intArr(i) = Mid(CStr(intIn), i, 1)
    Next i

    BubbleSort intArr

    For i = 1 To 6
        strResult = strResult + CStr(intArr(i))
    Next i

    SortNumber = strResult
End Function

Function BubbleSort(TempArray As Variant)
    Dim Temp As Variant
    Dim i As Integer
    Dim NoExchanges As Integer

    Do
        NoExchanges = True
        For i = 1 To UBound(TempArray) - 1
            If TempArray(i) > TempArray(i + 1) Then
                NoExchanges = False
                Temp = TempArray(i)
                TempArray(i) = TempArray(i + 1)
                TempArray(i + 1) = Temp
            End If
        Next i
    Loop While Not (NoExchanges)
End Function