集合的集合传递给函数,导致 ByRef 参数类型不匹配

问题描述 投票:0回答:1

我有4个集合,其中两个包含一些与训练代码相关的字符串。另外两个集合包含许多包含员工 ID 和员工姓名的集合。这些子集合之一还包括培训代码和员工上次完成培训的日期。

我正在尝试查找尚未完成的培训,因此我正在检查员工集合和培训代码集合,检查最后完成的培训集合中的条目。如果没有找到条目,我会创建一个集合,其中包含员工的 ID、姓名、培训代码以及指示他们尚未完成培训的字符串,然后将此信息添加到需要完成的培训集合中。

我遇到的问题是,即使所有数据类型都已正确声明,调用 MissingTraining() 函数时也会出现“ByRef 参数类型不匹配”错误。该错误以黄色突出显示 Function GetItmsIncomplete() as Collection 行以及 If MissingTraining(employee, trainCode, ItmsInfo) Then 行中的“employee”一词。我已通过本地检查器确认传递的每个参数都是正确的数据类型,所以我很困惑为什么会弹出此错误。

主要功能如下:

Function GetItmsIncomplete(ByRef ItmsInfo As Collection, ByRef GemsInfo As Collection, ByRef ItmsRecurrent As Collection, ByRef ItmsStatic As Collection) As Collection
    
    ' ItmsInfo is collection of collection[6-digit-id,"F.Name L.Name",TrainCode,DateLastCompleted]
    ' GemsInfo is collection of collection[6-digit-id,9-digit-id,"L.Name, F.Name"]
    ' ItmsRecu is collection of TrainCodes that are recurrent. This list is meant to be compared using Left(comparisonCode,Len(thisCode)) for proper comparison
    ' ItmsStat is collection of TrainCodes that are one-time trainings. These values are meant to be compared using Left(comparisonCode,Len(thisCode)) for proper comparison
    
    Dim ItmsDue As New Collection
    Dim TrainingInfo As New Collection
    
    ' ItmsInfo does not contain incomplete training, so we need to check all employees in Gems, all codes from Static, and compare to Info to find incomplete training
    For Each employee In GemsInfo
        For Each trainCode In ItmsStatic
            If MissingTraining(employee, trainCode, ItmsInfo) Then
                Set TrainingInfo = Nothing
                TrainingInfo.Add employee(1)
                TrainingInfo.Add employee(3)
                TrainingInfo.Add trainCode
                TrainingInfo.Add "Incomplete"
                ItmsDue.Add TrainingInfo
            End If
        Next trainCode
        ' also grab incomplete Recurrent training
        For Each trainCode In ItmsRecurrent
            ' TR* and [REDACTED]* training is not required for everyone, so skip it
            If Left(trainCode, 2) <> "TR" And Left(trainCode, 7) <> "[REDACTED]" Then
                If MissingTraining(employee, trainCode, ItmsInfo) Then
                    Set TrainingInfo = Nothing
                    TrainingInfo.Add employee(1)
                    TrainingInfo.Add employee(3)
                    TrainingInfo.Add trainCode
                    TrainingInfo.Add "Incomplete"
                    ItmsDue.Add TrainingInfo
                End If
            Else
                ' TODO - TR-* training is required for those that have completed TR00*
            End If
        Next trainCode
    Next employee
    
    Set GetItmsIncomplete = ItmsDue
    
End Function

这是 MissingTraining() 函数:

Function MissingTraining(ByRef employee As Collection, trainCode As Variant, ByRef ItmsInfo As Collection) As Boolean
    ' employee [ItmsId,LmsId,"L.Name, F.Name"]
    ' training [ItmsId,"F.Name L.Name",TrainCode,LastComp]
    For Each training In ItmsInfo
        If training(1) = employee(1) And Left(training(3), Len(trainCode)) = trainCode Then
            MissingTraining = False
            Exit Function
        End If
    Next training
    MissingTraining = True
End Function

我尝试用谷歌搜索与集合集合相关的错误,但没有看起来相关的结果。我还在 stackoverflow 中搜索了一个看起来相关的问题,但我没有看到任何提及与集合集合相关的 byref 不匹配错误的内容。

我还尝试将整个员工集合更改为仅将员工 ID 作为字符串传递,但得到了完全相同的结果,包括“员工”一词上的蓝色突出显示。

奇怪的是,在运行不同的训练列表时,我以完全相同的方式将这组数据传递给项目前一部分中的类似函数,并且没有任何错误。我很困惑这与工作部分有何不同。 (是的,我已经确认我已经在前一部分中以完全相同的方式声明、循环、传递和 ByRef 了完全相同的事情)所以我只是感到困惑。

excel vba pass-by-reference
1个回答
0
投票

您尚未在

employee
中声明
GetItmsIncomplete
(而且它不是全局的)
如果您使用
Option Explicit
,您会收到编译时错误警告。

如果没有

Option explicit
employee
会旋转为
Variant
,因此无法将其传递给期望该参数为
Collection
类型的方法。

始终使用

Option Explicit
将使您避免此类问题。

© www.soinside.com 2019 - 2024. All rights reserved.