今日学习目标

  • 理解Collection对象
  • 掌握集合的基本操作
  • 了解集合与数组的区别

知识点

Collection对象

vba
Dim col As New Collection col.Add "item", "key" col(i) ' 按索引访问 col("key") ' 按键访问

今日代码

vba
Dim col As New Collection col.Add "item", "key" col(i) ' 按索引访问 col("key") ' 按键访问

练习题

  1. 创建并操作集合
  2. 遍历集合元素
  3. 使用集合去重

参考解答

1. 用Dim col As New Collection创建集合,使用Add方法添加元素,Remove删除,Count获取数量。 2. 使用For i = 1 To col.Count循环遍历,通过col(i)访问每个元素。 3. 遍历原数组逐个添加到集合,集合自动忽略重复元素,实现快速去重。


由在线工具箱(www.vba.net)整理制作