2013-04-22 60 views
2

我有兩個數組,我試圖提取它們中的公共值,函數findUniques應該是返回字符串(它們是名字)連接在一起並用逗號分隔。我想我很接近,但我找不到什麼不對。我得到一個錯誤在第10行賦值左側的函數調用必須返回Variant或Object,在兩個數組中找到公共值

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 

回答

0

這裏來聲明一個變量來保存您正在構建

昏暗tempString作爲字符串

字符串,並在第10行添加此 tempString = tempString & 「」 & astrArray1(計數器1)

最後 findUniques = tempString 結束功能

+0

完美!感謝您的幫助 – user2217895 2013-04-22 21:48:57

+0

Kenneth在回答中可能確定了問題的原因。雖然目前的答案解決了OP的問題,但可能是由於刪除了導致錯誤的行。本文旨在說明使用輔助字符串變量來解決問題並不是強制性的。 – 2017-01-04 23:06:01

2

你的錯誤是在這一行:

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 

雖然這將解決您的編譯器錯誤,你需要修改你的函數,因爲這不是很合乎邏輯。無論如何,嘗試一下,如果您有任何其他問題,您可以再次

相關問題