Excel MAXIF 函数或仿真?
我在 excel 中有一个中等大小的数据集,我从中希望提取 B 列中值的最大值,但这些值仅对应于 A 列中满足特定条件的单元格。
所需的功能与 SUMIF
或 COUNTIF
类似,但它们都不会返回必需的数据。没有 MAXIF 函数;我如何模仿一个?
I have a moderately sized dataset in excel from which I wish to extract the maximum value of the values in Column B, but those that correspond only to cells in Column A that satisfy certain criteria.
The desired functionality is similar to that of SUMIF
or COUNTIF
, but neither of those return data that is necessary. There isn't a MAXIF
function; how do I emulate one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用数组公式。在要计算最大值的单元格中输入:=Max(If([test],[if true],[if false]),其中用测试替换方括号中的值,如果为 true,则返回什么;如果为 false,则返回什么。 例如:
在此公式中,如果值除以 2 没有余数,则返回 A 列中的值。请注意,我在比较和值中使用了一系列单元格。 。
现在,在编辑单元格时,按 Ctrl+Shift+Enter(同时按住 Ctrl 键和 Shift 键,然后按 Enter),
这将创建一个作用于单元格中的每个值的数组公式 ?
编辑 顺便说一句,您想以编程方式还是手动执行此操作?如果以编程方式,那么您使用什么环境?
编辑 如果通过 VBA,您需要什么 使用 FormulaArray 属性和 R1C1 引用,如下所示:
You can use an array formula.In the cell in which you want the max calculated enter: =Max(If([test],[if true],[if false]) where you replace the values in square brackets with the test, what to return if true and what to return if false. For example:
In this formula I return the value in column A if the value divided by 2 has no remainder. Notice that I use a range of cells in my comparison and in the value if false rather than a single cell.
Now, while still editing the cell, hit Ctrl+Shift+Enter (hold down the Ctrl key and the Shift together and then hit enter).
This creates an array formula that acts on each value in the range.
EDIT BTW, did you want to do this programmatically or manually? If programmatically, then what environment are you using? VBA? C#?
EDIT If via VBA, you need to use the FormulaArray property and R1C1 references like so:
当您想要使用动态或命名范围时(例如,“当前行上方与当前行具有相同交易对手的行的最大应付金额),数组公式不能很好地工作。如果您不想使用对于数组公式,您始终可以求助于 VBA 来执行以下操作:
Array formulas don't work very well when you want to use dynamic or named ranges (e.g., "the maximum amount due for rows above the current row that have the same counterparty as the current row). If you don't want to use an array formula, you can always resort to VBA to do something like this:
到目前为止提供的代码的一个限制是您仅限于 2 个条件。我决定进一步改进此代码,不限制 MaxIfs 函数的条件数量。请参阅此处的代码:
此代码允许 1 到多个条件。
该代码是参考 Hans V 在 Eileen's Lounge 发布的多个代码而开发的。
快乐编码
Diedrich
A limitation with the code provided thus far is that you are restricted to 2 conditions. I decided to take this code further to not restrict the number of conditions for the MaxIfs function. Please see the code here:
This code allows 1 to multiple conditions.
This code was developed with reference to multiple code posted by Hans V over at Eileen's Lounge.
Happy coding
Diedrich