我有一些看起来像这样的代码:
pos.Clutch = sh2.Cells(R, Clutch)
pos.Wiper = sh2.Cells(R, Wiper)
pos.Alternator = sh2.Cells(R, Alternator)
pos.Compressor = sh2.Cells(R, Compressor)
...
pos.Telephone = sh2.Cells(R, Telephone)
poss.Add pos
Run Code Online (Sandbox Code Playgroud)
poss是一个集合,Clutch,Wiper等是列索引(从1开始).这目前有效,但非常难看.我正在寻找一种方法来做这样的事......
Do While i <= classProperty.count
For each classProperty in pos
classProperty = sh2.Cells(R + 1, i)
Next classProperty
Loop
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,但是有没有人对如何在类中完成大致相同的方法或集合有任何建议?
在我之前的问题中,如何为 VBA 中在运行时提供属性名称的属性赋值?,我学会了在运行时使用 CallByName 在类中设置属性。
然而,这一次,我试图弄清楚如何在运行时从字符串中获取对象。
例如,假设我有一个包含以下数据的字符串:Worksheets("RAW DATA").Range("A1").QueryTable.
这是我可能会尝试做的事情,上面的数据是strParam下面的输入:
Function GetObject(strParam As String) As Object
GetObject = SomeFunction(strParam)
End Function
Run Code Online (Sandbox Code Playgroud)
在这种情况下,GetObject 在对 进行评估时应该返回一个 QueryTable Worksheets("RAW DATA").Range("A1").QueryTable。VBA 中有什么东西可以代替SomeFunction上面的例子吗?
我正在尝试编写一个VBA函数,该函数根据对象属性之一的值计算集合中的对象.我需要将被检查的对象属性设置为动态的,由函数参数提供.我可以使用一个if then语句,但是这将有许多elseif条款,每个条款都有相同的过程,除了属性名称.
我想避免为每个属性名称反复重复我的代码.这是我到目前为止所拥有的.
Private Function getTicketCount(c As Collection, f As String, s As String) _
As Long
' @param c: collection of Ticket objects.
' @param f: property to filter.
' @param s: filter string.
'
' Function returns number of tickets that pass the filter.
Dim x As Long
Dim t As Ticket
x = 0
For Each t In c
If t.f = s Then x = x + 1 ' Compiler throws "Method …Run Code Online (Sandbox Code Playgroud)