在 VBA 中查找列的最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42633273/
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
Finding Max of a column in VBA
提问by nas
I am trying to find the max value of a cell within A2 to A999. I tried to do a For Loop to loop through each one, but having problems with the logic to find the max rcell. I was hoping there was a built-in rcell.max function?
我试图在 A2 到 A999 中找到单元格的最大值。我试图做一个 For 循环来遍历每个循环,但是在找到最大 rcell 的逻辑上遇到了问题。我希望有一个内置的 rcell.max 函数?
Set rng = loopset.range("A2-A999")
For Each rcell in rrng.cells
'Find the max rcell
Next rcell
回答by nwhaught
Application.worksheetfunction.max(range("a:a"))will do it for you
Application.worksheetfunction.max(range("a:a"))会为你做的
回答by Yoav24
You can indeed use the function application.worksheetfunction.max(rng) as noted. To give a bit more complete answer, almost ANY Excel formula that's available on the worksheet is available to use in the VBA code window via the application.worksheetfunction collection. This includes max, min, sumif, vlookup, etc.
您确实可以使用功能 application.worksheetfunction.max(rng) 如上所述。为了给出更完整的答案,工作表上可用的几乎所有 Excel 公式都可以通过 application.worksheetfunction 集合在 VBA 代码窗口中使用。这包括 max、min、sumif、vlookup 等。
This should give you the same on-screen description of the arguments involved in the function that you do when using the function on a worksheet. However, as another use noted, using application.max(range) does not give the same argument help. Also as same user noted, there's a difference in error handling between using application.max(rng) and worksheetfunction.max(rng), you can see this in the comment below
这应该为您提供与在工作表上使用该函数时所做的函数所涉及的参数相同的屏幕描述。然而,正如另一个用途所指出的,使用 application.max(range) 并没有提供相同的参数帮助。同样正如用户所指出的,使用 application.max(rng) 和 worksheetfunction.max(rng) 之间的错误处理存在差异,您可以在下面的评论中看到这一点
As for a programming logic to determine a max value from a list of values, the basic logic is this:
至于从值列表中确定最大值的编程逻辑,基本逻辑是这样的:
max = 0 'set "inital" max
For Each rcell in rrng.cells 'loop through values
if rcell.value > max then 'if a value is larger than the old max,
max = rcell.value ' store it as the new max!
Next rcell

