简体   繁体   English

Excel VBA在特定工作表上查找范围内的最大值

[英]Excel VBA find maximum value in range on specific sheet

I found that the following code is getting max value in range: 我发现以下代码在范围内获得最大值:

Cells(Count, 4)=Application.WorksheetFunction.Max(Range(Cells(m, 1),Cells(n, 1)))

How can I search within a specific sheet? 如何在特定工作表内搜索? Data sheet in this case 这种情况下的Data

Like: 喜欢:

Worksheets(d).Cells(x, 4).Value = Worksheets("Data")... ????? FIND MAX ????

This will often work, but is missing a reference: 这通常会起作用,但缺少参考:

 worksheets("Data").Cells(Count, 4)=  Application.WorksheetFunction.Max _
    ( worksheets("Data").range( cells(m,1) ,cells(n,1) )

The text "cells" should be preceded by a reference to which worksheet the cells are on, I would write this: 文本“单元格”之前应该是对单元格所在工作表的引用,我会这样写:

worksheets("Data").Cells(Count, 4) = Application.WorksheetFunction.Max _
    ( worksheets("Data").range( worksheets("Data").cells(m,1) ,worksheets("Data").cells(n,1) )

This can also be written like this which is clearer: 这也可以这样写得更清楚:

with worksheets("Data")
    .Cells(Count, 4) =  Application.WorksheetFunction.Max _
                            ( .range( .cells(m,1) ,.cells(n,1) )
End With 

I hope this helps. 我希望这有帮助。

Harvey 哈维

You can pass any valid excel cell reference to the range method as a string. 您可以将任何有效的excel单元格引用作为字符串传递给range方法。

Application.WorksheetFunction.Max(range("Data!A1:A7"))

In your case though, use it like this, defining the two cells at the edges of your range: 但是,在你的情况下,像这样使用它,在你的范围的边缘定义两个单元格:

Application.WorksheetFunction.Max _
    (range(worksheets("Data").cells(m,1),worksheets("Data").cells(n,1)))

If you want to process quickly milion of cells to find MAX/MIN, you need heavier machinery. 如果你想快速处理数百万个细胞以找到MAX / MIN,你需要更重的机器。 This code is faster then Application.WorksheetFunction.Max . 此代码比Application.WorksheetFunction.Max快。

Function Max(ParamArray values() As Variant) As Variant
   Dim maxValue, Value As Variant
   maxValue = values(0)
   For Each Value In values
       If Value > maxValue Then maxValue = Value
   Next
   Max = maxValue
End Function

Function Min(ParamArray values() As Variant) As Variant
   Dim minValue, Value As Variant
   minValue = values(0)
   For Each Value In values
       If Value < minValue Then minValue = Value
   Next
   Min = minValue
End Function

Stolen from here: https://www.mrexcel.com/forum/excel-questions/132404-max-min-vba.html 从这里被盗: https//www.mrexcel.com/forum/excel-questions/132404-max-min-vba.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM