赋值左侧的函数调用必须返回Variant或Object,在两个数组中查找公共值

时间:2013-04-22 21:33:42

标签: arrays string vba excel-vba for-loop

我有两个数组,我正在尝试提取它们中的公共值,函数findUniques应该返回字符串(它们的名称)连接并用逗号分隔。我想我很接近,但我找不到什么是错的。我在第10行收到错误

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

    Dim blnMP5 As Boolean
    blnMP5 = False
    Dim counter1 As Long
    Dim counter2 As Long

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

End Function

2 个答案:

答案 0 :(得分:3)

您的错误就在这一行:

findUniques() = findUniques & "," & "astrArray1()"

您正在尝试为函数调用赋值,这没有意义。

您应该在函数的开头声明一个变量,并继续添加到该函数并最终返​​回它:

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

    Dim blnMP5 As Boolean
    blnMP5 = False    
    Dim counter1 As Long
    Dim counter2 As Long  
    Dim uniquesString as String = ""

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

End Function

答案 1 :(得分:0)

声明一个变量来保存你正在构建的字符串

将tempTtring变为字符串

并在第10行添加 tempString = tempString& “,”& astrArray1(计数器1)

最后 findUniques = tempString 结束功能