所以我创建了下面的代码,这是一个接受CReliability类(类模块中的自制类)的两个不同变量的函数。它还接受两个不同的集合,每个集合都是两个集合的一部分。此函数旨在将rawRel变量(CReliability类型)的每个属性中的所有唯一项分配给newCol (集合)中的newRel变量(CReliability类型),并计算每个属性中每个唯一项出现的次数。我想扩展这个想法,这样我就可以为我想要访问的任何属性实现这个函数,获取唯一项和计数。但是,我在使用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类代码。
发布于 2018-03-01 03:40:36
您可以使用
CallByName(newRel, "defect", VbGet)来获取值,但显然不能将其用作LValue (即为其赋值)。
试一试
CallByName newRel, "defect", VbLet, currhttps://stackoverflow.com/questions/49037215
复制相似问题