VBA调用MissingTraining函数出现ByRef参数类型不匹配错误排查
问题:VBA集合嵌套调用时出现ByRef参数类型不匹配错误
我有4个Collection集合:
- 两个存储培训代码相关字符串(
ItmsRecurrent、ItmsStatic) - 另外两个是包含员工ID、姓名的子集合:
GemsInfo:子集合结构为[6位ID, 9位ID, "姓氏, 名字"]ItmsInfo:子集合结构为[6位ID, "名字 姓氏", 培训代码, 最近完成日期]
需求是找出未完成的培训:遍历员工集合和培训代码集合,检查已完成培训集合(ItmsInfo)中是否存在对应记录;若不存在,则生成包含员工ID、姓名、培训代码及Incomplete标识的子集合,添加到待完成培训集合(ItmsDue)。
但调用MissingTraining()函数时,出现**ByRef argument type mismatch参数类型不匹配**错误,错误指向GetItmsIncomplete()函数声明行以及调用时的employee参数。我已通过Locals窗口确认所有参数类型均正确,尝试改为仅传递员工ID字符串仍出现相同错误。
我搜索过集合嵌套场景的该错误,未找到相关结果;也查阅了类似问题,未发现涉及集合嵌套的ByRef不匹配案例。此前项目使用完全相同的数据传递逻辑处理其他培训列表时未出错,对这个差异感到困惑。
相关代码
主函数GetItmsIncomplete
Function GetItmsIncomplete(ByRef ItmsInfo As Collection, ByRef GemsInfo As Collection, ByRef ItmsRecurrent As Collection, ByRef ItmsStatic As Collection) As Collection ' ItmsInfo 是子集合的集合,子集合结构:[6位ID,"名字 姓氏",培训代码,最近完成日期] ' GemsInfo 是子集合的集合,子集合结构:[6位ID,9位ID,"姓氏, 名字"] ' ItmsRecurrent 是循环培训代码的集合,比较时需用Left(comparisonCode,Len(thisCode))匹配 ' ItmsStatic 是一次性培训代码的集合,比较时需用Left(comparisonCode,Len(thisCode))匹配 Dim ItmsDue As New Collection Dim TrainingInfo As New Collection ' ItmsInfo 中没有未完成培训记录,所以需要遍历Gems中的所有员工、Static中的所有代码,和Info对比找出未完成项 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 ' 同时找出未完成的循环培训 For Each trainCode In ItmsRecurrent ' TR* 和 [REDACTED]* 培训不是全员要求,跳过 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-* 培训仅要求已完成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,"姓氏, 名字"] ' training 子集合结构:[ItmsID,"名字 姓氏",培训代码,最近完成日期] 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
内容的提问来源于stack exchange,提问作者Chasenuva1
相关产品推荐
相关产品推荐

