arrays - How To Rank A Multi Dimensional Array In VBA
问题描述
I'm embarrassed I couldn't find this answer on my own, but if I have a multi-dimensional VBA array, and wish to use the Rank/Large/Small
function on a single dimension, how is that executed?
Example:
Dim Arr(1, 2) As Integer
Arr(0, 0) = 1
Arr(0, 1) = 2
Arr(0, 2) = -1
Arr(1, 0) = 100
Arr(1, 1) = 40
Arr(1, 2) = 60
Here's a visual illustration:
If I want the largest number in the Arr(0, x) field (which would be 2), how is that executed? For the largest in the Arr(1,x) field I would expect 100.
I've tried:
Application.WorksheetFunction.Large(Arr(, 2), 1)
this errors
Application.WorksheetFunction.Large(Arr(1, 2), 1)
this gives just that array on that postion.
I've googled:
- ranking multi-dimensional arrays
- how to rank a single dimension an array
- rank one dimension in array
No luck. I could probably find it, but I'm also hoping I might get a -3 ranking on this weak question so I get can delete it and earn the SO "humiliation" badge (or whatever it's called).
Thanks!
解决方案
You need to peel out one of the ranks with the worksheet's Index function.
Dim Arr(1, 2) As Integer
Arr(0, 0) = 1
Arr(0, 1) = 2
Arr(0, 2) = -1
Arr(1, 0) = 100
Arr(1, 1) = 40
Arr(1, 2) = 60
Debug.Print Application.Large(Application.Index(Arr, 0, 2), 1)
Index is used as all the 'rows' (0) in the second 'column' (2). Between 2 and 40 in the second rank, 40 is the largest.
推荐阅读
- excel - Excel 结构化引用过滤表并生成列表
- javascript - 如何在 jQuery 中使用 array_key_exists() 和 ksort()?
- python - Django:通过 SQL 过滤,而不是 Python
- c# - 如何检查字符串是否是有效的子域
- javascript - JavaScript - 将用户输入文本字段添加到 th/td 元素
- r - 有一个 R 库可以在地图上创建网格吗?
- python - 将 excel 转换为 CSV 的 Python 代码 - 条件在 PYCHARM 中有效,但在 shell python 中因语法无效而失败
- python-3.x - 使用 Imblearn 管道和 GridSearchCV 进行交叉验证
- html - 如何放置列表标记(标签
- ) 如果它由几行组成,则与 li 段落的第一行相对
- c# - 如何使用 DirectLine 设置 [Activity.From.Id] 、 [member.Id] 或 [activity.Recipient.Id]?