vba:从数组中获取唯一值
vba 中是否有任何内置功能可以获取一维数组中的唯一值?只删除重复项怎么样?
如果没有,那么我如何从数组中获取唯一值?
Is there any built-in functionality in vba to get unique values from a one-dimensional array? What about just getting rid of duplicates?
If not, then how would I get the unique values from an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
没有内置功能可以从数组中删除重复项。 Raj的回答看起来很优雅,但我更喜欢使用字典。
编辑:我按照 Tomalak 的建议答案更改了循环以使用
LBound
和UBound
。编辑: d.Keys() 是一个 Variant 数组,而不是一个 Collection。
There's no built-in functionality to remove duplicates from arrays. Raj's answer seems elegant, but I prefer to use dictionaries.
EDIT: I changed the loop to use
LBound
andUBound
as per Tomalak's suggested answer.EDIT:
d.Keys()
is a Variant array, not a Collection.更新(2016 年 6 月 15 日)
我创建了更全面的基准测试。首先,正如 @ChaimG 指出的,早期绑定有很大的不同(我最初逐字使用上面的 @eksortso 的代码,它使用后期绑定)。其次,我原来的基准测试只包括创建唯一对象的时间,但是并没有测试使用该对象的效率。我这样做的目的是,如果我创建的对象很笨重并且减慢了我前进的速度,那么我是否可以非常快地创建一个对象并不重要。
旧评论:事实证明,对集合对象进行循环效率非常低
事实证明,如果您知道如何执行此操作(我不知道),则对集合进行循环可能会非常高效。正如@ChaimG(再次)在评论中指出的那样,使用
For Each
构造比简单地使用For
循环优越得多。为了让您了解一下,在更改循环构造之前,Test Case Size = 10^6
的Collection2
的时间超过 1400 秒(即约 23 分钟)。现在仅为 0.195 秒(快了 7000 多倍)。对于
Collection
方法有两次。第一个(我的原始基准Collection1
)显示创建唯一对象的时间。第二部分 (Collection2
) 显示了循环对象(这是非常自然的)以创建可返回数组的时间,就像其他函数一样。在下图中,黄色背景表示该测试用例最快,红色表示最慢(不包括“未测试”算法)。
Collection
方法的总时间是Collection1
和Collection2
的总和。绿松石色表示无论原始顺序如何,它都是最快的。下面是我创建的原始算法(我对其进行了轻微修改,例如我不再实例化我自己的数据类型)。它会在非常短的时间内返回具有原始顺序的数组的唯一值,并且可以对其进行修改以采用任何数据类型。除了
IndexMethod
之外,它是处理超大型数组的最快算法。以下是该算法背后的主要思想:
下面是一个示例:
这是代码:
这是我使用的排序算法(有关此算法的更多信息此处)。
下面是一种特殊的算法,如果您的数据包含整数,该算法的速度会非常快。它利用索引和布尔数据类型。
以下是集合(由@DocBrown)和字典(由@eksortso)函数。
这是@IsraelHoletz 提供的直接方法。
这是比较所有函数的基准函数。您应该注意,由于内存问题,最后两种情况的处理方式略有不同。另请注意,我没有测试
Test Case Size = 10,000,000
的Collection
方法。由于某种原因,它返回了不正确的结果并且行为异常(我猜测集合对象对可以放入其中的东西有限制。我进行了搜索,但找不到任何关于此的文献)。最后,这是生成上表的子程序。
总结
从结果表中,我们可以看到
Dictionary
方法对于小于 500,000 的情况非常有效,但是,在那之后,IndexMethod
真正开始占据主导地位。您会注意到,当顺序无关紧要并且数据由正整数组成时,与IndexMethod
算法没有比较(它从包含 1000 万个元素的数组中返回唯一值,时间更短)不到 1 秒!!!)。下面我详细介绍了在各种情况下首选哪种算法。案例1
您的数据包含整数(即整数,包括正数和负数):
IndexMethod
情况 2
您的数据包含元素少于 200000 个的非整数(即变体、双精度、字符串等):
字典方法
情况 3
您的数据包含超过 200000 个元素的非整数(即变体、双精度、字符串等):
Collection Method
如果您必须选择一种算法,在我看来,
Collection< /code> 方法仍然是最好的,因为它只需要几行代码,它非常通用,而且足够快。
Update (6/15/16)
I have created much more thorough benchmarks. First of all, as @ChaimG pointed out, early binding makes a big difference (I originally used @eksortso's code above verbatim which uses late binding). Secondly, my original benchmarks only included the time to create the unique object, however, it did not test the efficiency of using the object. My point in doing this is, it doesn't really matter if I can create an object really fast if the object I create is clunky and slows me down moving forward.
Old Remark: It turns out, that looping over a collection object is highly inefficient
It turns out that looping over a collection can be quite efficient if you know how to do it (I didn't). As @ChaimG (yet again), pointed out in the comments, using a
For Each
construct is ridiculously superior to simply using aFor
loop. To give you an idea, before changing the loop construct, the time forCollection2
for theTest Case Size = 10^6
was over 1400s (i.e. ~23 minutes). It is now a meager 0.195s (over 7000x faster).For the
Collection
method there are two times. The first (my original benchmarkCollection1
) show the time to create the unique object. The second part (Collection2
) shows the time to loop over the object (which is very natural) to create a returnable array as the other functions do.In the chart below, a yellow background indicates that it was the fastest for that test case, and red indicates the slowest ("Not Tested" algorithms are excluded). The total time for the
Collection
method is the sum ofCollection1
andCollection2
. Turquoise indicates that is was the fastest regardless of original order.Below is the original algorithm I created (I have modified it slightly e.g. I no longer instantiate my own data type). It returns the unique values of an array with the original order in a very respectable time and it can be modified to take on any data type. Outside of the
IndexMethod
, it is the fastest algorithm for very large arrays.Here are the main ideas behind this algorithm:
Below is an example:
Here is the code:
Here is the sorting algorithm I use (more about this algo here).
Below is a special algorithm that is blazing fast if your data contains integers. It makes use of indexing and the Boolean data type.
Here are the Collection (by @DocBrown) and Dictionary (by @eksortso) Functions.
Here is the Direct approach provided by @IsraelHoletz.
Here is the benchmark function that compares all of the functions. You should note that the last two cases are handled a little bit different because of memory issues. Also note, that I didn't test the
Collection
method for theTest Case Size = 10,000,000
. For some reason, it was returning incorrect results and behaving unusual (I'm guessing the collection object has a limit on how many things you can put in it. I searched and I couldn't find any literature on this).And finally, here is the sub that produces the table above.
Summary
From the table of results, we can see that the
Dictionary
method works really well for cases less than about 500,000, however, after that, theIndexMethod
really starts to dominate. You will notice that when order doesn't matter and your data is made up of positive integers, there is no comparison to theIndexMethod
algorithm (it returns the unique values from an array containing 10 million elements in less than 1 sec!!! Incredible!). Below I have a breakdown of which algorithm is preferred in various cases.Case 1
Your Data contains integers (i.e. whole numbers, both positive and negative):
IndexMethod
Case 2
Your Data contains non-integers (i.e. variant, double, string, etc.) with less than 200000 elements:
Dictionary Method
Case 3
Your Data contains non-integers (i.e. variant, double, string, etc.) with more than 200000 elements:
Collection Method
If you had to choose one algorithm, in my opinion, the
Collection
method is still the best as it only requires a few lines of code, it's super general, and it's fast enough.从 365 开始是这样的。您可以使用 UNIQUE,但需要先对其进行 TRANSPOSE,除非您的输入是 RANGE。并排序以获取乐趣。
As of 365 it does. You can use UNIQUE, but you'll need to TRANSPOSE it first unless your input is a RANGE. And SORT for fun.
我不知道VBA中有任何内置功能。最好的方法是使用以值作为键的集合,并且仅在值不存在时才添加到集合中。
I don't know of any built-in functionality in VBA. The best would be to use a collection using the value as key and only add to it if a value doesn't exist.
不,没有任何内置的东西。自己动手:
Scripting.Dictionary
对象For
循环(请务必使用LBound()
和UBound()
而不是从 0 到 x 循环!)Exists()
。将每个数组值(尚不存在)添加为字典的键(使用正如我刚刚了解到的,键可以是 Scripting.Dictionary 中的任何类型),还将数组值本身存储到字典中。CStr()
因为键必须是字符串Keys()
(或Items()
)将字典的所有值作为新的、现在唯一的数组返回。No, nothing built-in. Do it yourself:
Scripting.Dictionary
objectFor
loop over your array (be sure to useLBound()
andUBound()
instead of looping from 0 to x!)Exists()
on the dictionary. Add every array value (that doesn't already exist) as a key to the dictionary (useas I've just learned, keys can be of any type in aCStr()
since keys must be stringsScripting.Dictionary
), also store the array value itself into the dictionary.Keys()
(orItems()
) to return all values of the dictionary as a new, now unique array.使用 stdVBA (一个主要由我自己维护的库),您可以使用:
注释:
您还可以获得某些
Collection
/任何实现IEnumVARIANT
的对象的Unique
:您还可以获得
由某个对象的属性唯一
:With stdVBA (a library largely maintained by myself) you can use:
Notes:
You can also get
Unique
for someCollection
/ any object which implementsIEnumVARIANT
:You can also get
Unique
by some object's property:不,VBA 没有此功能。您可以使用将每个项目作为键添加到集合中的技术。由于集合不允许重复的键,因此结果是不同的值,您可以根据需要将其复制到数组中。
您可能还想要更强大的东西。请参阅 不同值函数,网址为 http://www.cpearson.com/ excel/distinctvalues.aspx
No, VBA does not have this functionality. You can use the technique of adding each item to a collection using the item as the key. Since a collection does not allow duplicate keys, the result is distinct values that you can copy to an array, if needed.
You may also want something more robust. See Distinct Values Function at http://www.cpearson.com/excel/distinctvalues.aspx
对于简短的方法来说,集合和字典解决方案都很好,很出色,但是如果您想要速度,请尝试使用更直接的方法:
调用它:
对于速度比较,这将比字典解决方案快 100 倍到 130 倍,大约快 8000 倍比集合版快 13000 倍。
The Collection and Dictionary solutions are all nice and shine for a short approach, but if you want speed try using a more direct approach:
Calling it:
For speed comparasion, this will be 100x to 130x faster then the dictionary solution, and about 8000x to 13000x faster than the collection one.
没有用于从数组中删除重复项的 VBA 内置功能,但是您可以使用下一个函数:
There is no VBA built in functionality for removing duplicates from an array, however you could use the next function:
如果去重数组的顺序对你来说不重要,你可以使用我的实用函数:
从测试函数中,它将产生以下去重数组:
“0=Dear 1=Horse 2=Joke 3=Cow”
If the order of the deduplicated array does not matter to you, you can use my pragmatic function:
From the test function, it will result in the following deduplicated array:
"0=Dear 1=Horse 2=Joke 3=Cow "
我出于自己的目的而寻找这个,但不喜欢使用循环遍历每个元素、集合/字典或工作表函数。我制作了这个解决方案,仅循环遍历数组以获得相同数量的唯一项目。不过,我还没有运行任何基准测试来测试其与此处解决方案的速度。
我的看法:
I was looking for this for my own purposes but didn't like the use of looping through each element, or collections/dictionaries, or the worksheet function. I made this solution that only loops through the array for the same number of unique items. I haven't run any benchmarks to test its speed against the solutions here though.
My take:
这篇文章包含 2 个示例。我喜欢第二个:
This post contains 2 examples. I like the 2nd one: