Excel VBA:如何在忽略错误单元格的同时查找范围的最大值/最小值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12327277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:38:29  来源:igfitidea点击:

Excel VBA: How to find max/min of a range while ignoring error cells

excelvbaexcel-formula

提问by user1656007

How can I use VBA in Excel to determine the max/min of a range that contains error cells such as #N/Aor empty cells? I know this is a fairly easy task to conquer with Excel array formulas using something like
=MIN(IF(A1:A10="#N/A"))

如何在 Excel 中使用 VBA 来确定包含错误单元格(例如#N/A或空单元格)的范围的最大值/最小值?我知道这是一项相当容易的任务,可以使用类似的东西来克服 Excel 数组公式
=MIN(IF(A1:A10="#N/A"))

but I would very much like to accomplish this using VBA.

但我非常想使用 VBA 来实现这一点。

I'm dealing with several thousand lines of data so the speediest solution would be preferred.

我正在处理数千行数据,因此最好使用最快速的解决方案。

Thank you so much!

非常感谢!

回答by brettdj

You can use Evaluateor the shortcut []to return the VBA equivalent of a formula

您可以使用Evaluate或 快捷方式[]返回公式的 VBA 等效项

So the Excel array formula
=MIN(IF(NOT(ISNA(A1:A10)),A1:A10))can be used in code like

所以Excel数组公式
=MIN(IF(NOT(ISNA(A1:A10)),A1:A10))可以在代码中使用

Sub Test()
MsgBox [MIN(IF(NOT(ISNA(A1:A10)),A1:A10))]
End Sub