如果2D阵列中的值存在于另一个2D阵列函数中

时间:2017-11-09 18:28:45

标签: excel vba excel-vba function multidimensional-array

我需要一个函数来帮助我确定2D数组中的值是否存在于另一个2D数组中。我试图重构一个在question中有效的函数。我遇到了错误Byref argument type mismatch(此后我添加了ByVal语句)以及我面临的当前错误function call on left-hand side of assignment

Public aLogic As Variant
Public Field_List(1 To 70, 1 To 10) As String, Field_No_of_Rows As Long

Sub Implement_Mapping()
Dim aMapRow As Integer, aMapCol As Integer

    For aMapRow = LBound(aLogic, 1) To UBound(aLogic, 1)
         For aMapCol = LBound(aLogic, 2) To UBound(aLogic, 2)

            If IsInArrayByVal(aLogic(aMapRow, aMapCol), Field_List) = True Then
                Debug.Print aLogic(aMapRow, aMapCol)
                     'For Each Break In ObjLSL

                     'Next
             End If
        Next aMapCol
    Next aMapRow

End Sub

Function IsInArrayByVal(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0)) 

2 个答案:

答案 0 :(得分:3)

假设您的其余代码有效,您必须更正以下内容:

Function IsInArrayByVal(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
    IsInArray = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0))
End Function

为:

Function IsInArrayByVal(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
    IsInArrayByVal = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0))
End Function

您的代码中的其他位置可能有IsInArray个功能,导致您提到的错误消息,即function call on left-hand side of assignment

答案 1 :(得分:1)

我的猜测是,Field_List(1 To 70, 1 To 10) As String应为Field_List(1 To 70, 1 To 10) As Integer。将数值类型与非数字类型进行比较时,您会遇到类型不匹配。

另一个奇怪的是你有

Function IsInArrayByVal(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0)) 

没有End Function。也许你忘了将它复制到这篇文章中,但如果没有,我相当肯定会给你带来问题。

所以它应该是:

Function IsInArrayByVal(ByVal stringToBeFound As String, ByVal arr As Variant) As Boolean

    IsInArrayByVal = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0)) 

End Function