有集合时为什么要在 VBA 中使用数组? [英] Why use arrays in VBA when there are collections?
问题描述
许多人在 Excel/VBA 中广泛使用数组来存储数据列表.但是,在我看来,有一个集合对象要方便得多(主要是:不需要重新/定义列表的长度).
many people use extensively arrays in Excel/VBA to store a list of data. However, there is the collection object which in my view is MUCH MUCH more convenient (mainly: don't need to re/define length of the list).
所以,我真诚地问自己是否遗漏了什么? 为什么其他人仍然使用数组来存储数据列表?难道只是过去的宿醉?
So, I am sincerely asking myself if I am missing something? Why do other people still use arrays to store a list of data? Is it simply a hangover of the past?
推荐答案
使用数组而不是集合(或字典)的几个原因:
Several reasons to use arrays instead of collections (or dictionaries):
- 您可以使用
Range("A1:B12") = MyArray
轻松地将数组转换为范围(反之亦然) - 集合只能存储唯一键,而数组可以存储任何值
- 集合必须存储一对(键,值),而您可以将任何内容存储在数组中
- you can transfer easily array to range (and vice-versa) with
Range("A1:B12") = MyArray
- collections can store only unique keys whereas arrays can store any value
- collections have to store a couple (key, value) whereas you can store whatever in an array
请参阅 Chip Pearson 的文章关于数组以更好地理解
See Chip Pearson's article about arrays for a better understanding
更好的问题是为什么人们会使用集合而不是字典(好吧,集合是标准的 VBA 而你必须导入字典)
A better question would rather be why people would use collections over dictionaries (ok, collections are standard VBA whereas you have to import dictionaries)
这篇关于有集合时为什么要在 VBA 中使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!