Excel 查找速度与 VBA 二进制搜索?
Excel VBA 的查找与二分搜索相比有多好/快?我的平台是 Office 11|2003,我将在三张值上针对 A 列搜索字符串。总行数 ~140,000
如果值得哪个库和?我应该引用函数来进行排序然后进行二分搜索?据报道,二进制搜索字符串/文本存在潜在问题。
...一件事 必须注意。使用二分查找 带有排序文本的公式需要 警告。 Aladin A.,Excel MVP
Excel 查找:
Worksheets(1).Range("A:A").Find("PN-String-K9", LookIn:=xlValues, LookAt:=xlWhole)
How good/fast is Excel VBA's Find vs. binary search? My platform is Office 11|2003 and I'll be searching for strings against Column A on three sheets of values. Total number of rows ~140,000
If worth it which Library & functions should I reference to do the sorting and then the binary search? Binary searching strings/text reportedly has potential problems.
... one thing
must be noted. Using binary search
formulas with sortedtextrequires
caution. Aladin A., Excel MVP
Excel Find:
Worksheets(1).Range("A:A").Find("PN-String-K9", LookIn:=xlValues, LookAt:=xlWhole)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与我的直觉相反,VBA 二分搜索的性能远远优于 Excel 查找。至少在下面的场景中,120,000 个 6 字符串均匀分布在 3 个工作表上。
Excel 查找需要 1 分 58 秒,
在我的特定机器上,VBA 二分查找需要 36 秒。
知道文本按顺序排列的优势显然超过了 Excel 的天然优势。请注意 Aladin A 关于排序顺序的警告。
Much against my intuition a VBA binary search strongly outperforms an Excel Find. At least with the scenario below where 120,000 6 character strings are distributed evenly over 3 worksheets.
Excel Find takes 1 minute 58 seconds,
VBA binary search takes 36 seconds on my particular machine.
The advantage of knowing that the text is in order obviously outweighs Excel's natural advantage. Note Aladin A's warning about sort order.
我发现使用自动筛选比使用任何方法手动搜索记录要快得多。
我进行过滤,检查是否有任何结果,然后继续。如果找到任何内容(通过检查结果计数),我可以搜索手动过滤的一小部分或将其全部返回。
我在大约 44,000 条记录中使用了它,并根据它搜索了 100 多个部件的列表。
如果不小心,二分搜索很容易陷入无限循环。
I find using the AutoFilter works a lot faster than manually searching the records with any method.
I filter, check to see if there are any results, then move on. If any are found (by checking the count of results), I can search the small portion that are filtered manually or return them all.
I used it on around 44,000 records, searching for a list of 100+ parts against it.
Binary searches can easily get stuck in infinite loops if you're not careful.
如果您将 vlookup 与排序选项一起使用,它可能会比您的 vba 更快。
If you use vlookup with the sorted option, it will likely be faster than your vba.
我对此很感兴趣,因为我正在使用 .Find 功能,在一台电脑上它无法进行某些查找,但在另一台电脑上却没问题!所以我对计时做了一些测试 - 我有一张按顺序排序的 985 个名称的表,并且我编写了一个小子例程来运行它们,并使用不同的方法在同一个列表中查找每个名称(时间以毫秒为单位):
VLookup 的问题是它无法返回行号,除非您将其包含在表中。
这是我的二分搜索代码,我假设工作表有标题行,但您可以轻松修改标题和代码以传递该信息。可选的 Col 参数用于指示您是否需要行号或单元格的值。如果查找失败,该函数将返回 0(零)。
I have become interested in this because I was using the .Find function, and on one PC it failed to work on some lookups, but on another it was OK! So I've done some testing on timings - I've got a sheet with 985 names sorted into order, and I've written a small subroutine to run through them, and look each one up in the same list using a different method (the times are in milliseconds):
The problem with VLookup is that it can't return the row number unless you include it in your table.
Here is my code for the Binary search, I've assumed that the sheet has a header row, but you could easily modify the header and code to pass that information. The optional Col parameter is used to indicated if you want the row number, or a cell's value. The function returns 0 (zero) if the find fails.