I am trying to write a VBA code that cross checks all of the values in one array with another (X with Y) and to copy the value into a new array if it is duplicated (FinalResults). So far I have the following code and would greatly appreciate some guidance on how to write it correctly.
开发者_如何学编程Function lnArray(X as Variant, Y as Variant) As Variant
Dim counter1 As Integer
Dim data As Integer
Dim FinalResults() As Variant
For counter1 = 1 To Max
For Each data In X
If data.Value = Y.Value Then
counter1 = counter1 + 1
ReDim Preserve FinalResults(counter1)
FinalResults(counter1) = data.Value
End If
Next data
Next counter1
End Function
First thing I saw when I looked over the code was, that Max
is neither declared nor initialized. Even if I assume that Option Explicit
is not set, this for-next loop runs from 1 to 0, which means no loops at all. I guess that's not what you intended?
Second is, your FinalResult
array is declared locally in this function, hence no kind of result value can be returned to the calling function.
精彩评论