1

我有一个接近工作但不完全的代码。我有两个数组 astrArray1 和 astrArray2 ,函数 findUniques() 应该返回两个数组中唯一的字符串(名称)。我可以让代码返回它们共同的名称,但它不会为唯一的名称返回正确的值。任何帮助,将不胜感激!

这是代码:

Function findUniques(astrArray1() As String, astrArray2() As String) As String


Dim blnMP5 As Boolean
blnMP5 = True

Dim counter1 As Long
Dim counter2 As Long

Dim uniquesString As String
uniquesString = " "

For counter1 = LBound(astrArray1) To UBound(astrArray1)
  For counter2 = LBound(astrArray2) To UBound(astrArray2)
    If astrArray1(counter1) <> astrArray2(counter2) Then
      blnMP5 = False
    ElseIf astrArray1(counter1) = astrArray2(counter2) Then
      blnMP5 = True
    End If
    If blnMP5 = False Then
      uniquesString = uniquesString & "," & astrArray2(counter2)
    End If
  Next counter2
Next counter1

findUniques = uniquesString
End Function
4

1 回答 1

0

像这样?

Option Explicit

Sub Sample()
    Dim MyAr1(1 To 4) As String
    Dim MyAr2(1 To 2) As String

    MyAr1(1) = "aaa": MyAr1(2) = "bbb": MyAr1(3) = "ccc": MyAr1(4) = "ddd"
    MyAr2(1) = "aaa": MyAr2(2) = "bbb"

    '~~> This will give you ccc,ddd
    Debug.Print findUniques(MyAr1, MyAr2)
End Sub

Function findUniques(astrArray1() As String, astrArray2() As String) As String
    Dim i As Long, j As Long
    Dim uniquesString As String
    Dim matchFound As Boolean

    For i = LBound(astrArray1) To UBound(astrArray1)
        matchFound = False
        For j = LBound(astrArray2) To UBound(astrArray2)
            If astrArray1(i) = astrArray2(j) Then
                matchFound = True
                Exit For
            End If
        Next j

        If matchFound = False Then uniquesString = uniquesString & "," & astrArray1(i)
    Next i

    findUniques = Mid(uniquesString, 2)
End Function
于 2013-04-24T20:10:45.053 回答