excel - 如何获取 Excel VBA 集合中单个项目的密钥
问题描述
我正在尝试编写一个通用排序,它将对键上的 VBA 集合进行排序。我在这里使用排序作为起点。
如果集合包含对象(如下所示),则排序不起作用。如何修改代码以获取用于排序的密钥?
我能够找到的信息获取键的集合,而不是获取单个项目的键(例如这里:VBA 集合:键列表)。
下面显示的当前代码在 Debug.print 行上给出了“对象不支持此属性或方法”。
Public Function sort(ByRef col As VBA.collection)
For i = 1 To col.Count - 1
For j = i + 1 To col.Count
Debug.Print ("Key: " & col(i))
If col(i) > col(j) Then
'store the lesser item
temp = col(j)
'remove the lesser item
col.Remove j
're-add the lesser item before the greater item
col.Add temp, temp, i
End If
Next j
Next i
End Function
Sub test()
Dim col As collection
Set col = New collection
Dim pi As ProgramIncrement
' val
Set pi = New ProgramIncrement
pi.name = "foo3"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo0"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo2"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo1"
col.Add pi, pi.name
' sort
sort col
Debug.Print ("Done.")
End Sub
' ---------------------------------------------------------------
'
' Class to represent Program Increment
'
' ---------------------------------------------------------------
Public name As String
Public sprints As New collection
解决方案
推荐阅读
- oauth-2.0 - 多租户 microsoft graph 应用程序管理员同意,无需代码回发
- django - 在 Django 中,是否可以在登录某些页面时混淆或隐藏客户端 IP?
- c - 从 C 程序与 gnuplot 交互。如何输入要绘制的数组
- javascript - 忽略正则表达式末尾的特殊字符
- postgresql - 直接查询表和查询返回同一张表的函数的区别
- python - 如何确保在小矩形碰到任何边界后立即显示文本,即屏幕的移动
- c# - 编写通过字符串返回所需列的选择器
- javascript - 如何在jQuery中选择元素
- reactjs - 将背景图像设置为 useState 变量会导致它变得未定义
- react-native - React-Native vesion 0.63 中的自定义标头组件(React-Native Naviagtion 文档中的混淆)