所以我创建了下面的代码,它是一个函数,它接受CReliability类的两个不同变量(类模块中的自制类)。它还包含两个不同的集合,每个集合都是分开的。此函数用于将rawRel变量(CReliability类型)的每个属性中的所有唯一项分配给newCol(Collection)中的newRel变量(CReliability Type),并计算每个属性的每个唯一项的出现次数。我想扩展这个想法,以便我可以为我想要访问的任何属性实现此功能,获取唯一项目和计数。我有问题使用VBA函数CallByName来执行此操作。我遇到的问题只有:
newRel.defect = curr 'This works but is not desired since it is a specific property
CallByName(newRel, "defect", VbGet) = curr 'This does not work and I want to work so I can use any property
这是整个功能的一部分:
Sub uniqueArr(ByRef newRel, ByRef rawRel, ByRef relCol, ByRef newCol, property As String)
For Each rawRel In relCol
curr = CallByName(rawRel, "defect", VbGet)
bool = False
For Each newRel In newCol
If CallByName(newRel, "defect", VbGet) = curr Then
bool = True
End If
Next
If Not bool Then
Set newRel = New CReliability
newRel.defect = curr 'This works but is not desired since it is a specific property
CallByName(newRel, "defect", VbGet) = curr 'This does not work and I want to work so I can use any property
newCol.Add newRel
End If
Next
For Each newRel In newCol
If newRel.defect = rawRel.defect Then
newRel.defectCount = newRel.defectCount + 1
End If
Next
End Sub
如果有人能帮助我理解为什么CallByName函数在这个实例中不起作用会很好。我还可以发布CReliability类代码,如果它有用的话。
答案 0 :(得分:3)
您可以使用
CallByName(newRel, "defect", VbGet)
获取值,但显然不能将其用作LValue(即为其分配内容)。
尝试
CallByName newRel, "defect", VbLet, curr