【问题标题】:If a value in a 2D array is present within another 2D array Function如果二维数组中的值存在于另一个二维数组中
【发布时间】:2018-04-22 21:00:03
【问题描述】:

我需要一个函数来帮助我确定二维数组中的值是否存在于另一个二维数组中。我试图重构以前在这个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)) 

【问题讨论】:

  • 使用调试/编译并在您的问题通过时更新。例如,Application.Match 不存在。
  • 您似乎想在这里问几个问题。通常,您应该针对每个帖子关注一个问题。
  • @Excelosaurus 确实如此,请参见此处:stackoverflow.com/questions/44159529/application-match-in-vba。不过,它可能不会在 Intellisense 中显示。
  • 匹配和索引现在都在 Application.WorksheetFunction 下。此外,函数中的左侧元素与函数名称不同,因此您的错误消息(您可能在其他地方有 IsInArray 函数)。

标签: vba excel function multidimensional-array


【解决方案1】:

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

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

【讨论】:

  • 我没听懂
【解决方案2】:

未经测试,我的猜测是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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多