vba 使用 Cells(x,y) 作为输入选择最大值 excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17198420/
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 21:45:52  来源:igfitidea点击:

selecting max value excel using Cells(x,y) as input

excel-vbamaxvbaexcel

提问by Mark C

How do you call the Max function in VBA using a range of Cells (x,y) as input?

如何使用一系列单元格 (x,y) 作为输入在 VBA 中调用 Max 函数?

E.g., I have two variables, m & n, where n > m

例如,我有两个变量,m & n,其中 n > m

I try to find the Max value within a range of cells using the following code:

我尝试使用以下代码在单元格范围内找到最大值:

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

Using that code I keep getting an error "Expected: list separator or )"

使用该代码我不断收到错误“预期:列表分隔符或)”

Edit, here is the entire code

编辑,这里是整个代码

Sub convertFNIRStoCandlesticks()

'Variable declarations
Dim rowCount As Integer             'The total number of rows in use
Dim Count As Integer
Dim Period As Integer
Dim totalPeriods As Integer
Dim PeriodStart As Integer
Dim PeriodEnd As Integer

rowCount = ActiveSheet.UsedRange.Rows.Count
totalPeriods = rowCount / 6
Sheets("Sheet1").Activate

For Count = 1 To totalPeriods
    Period = Count - 1
    PeriodStart = (Period * 6) + 1
    m = (Period * 6) + 1
    PeriodEnd = (Period * 6) + 6
    n = PeriodEnd
    Cells(Count, 2) = Cells(PeriodStart, 1)
    Cells(Count, 4) = Application.WorksheetFunction.Min(Range(Cells(PeriodStart, 1), Cells(PeriodEnd, 1)))
    Cells(Count, 5) = Cells(PeriodEnd, 1)
Next Count


End Sub

回答by Abe Gold

You can use the function Maxon Application.WorksheetFunctionwhere you use a Rangefrom Cells(m, 1)to Cells(n, 1):

您可以使用此功能MaxApplication.WorksheetFunction您使用其中一个Range来自Cells(m, 1)Cells(n, 1)

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

This will return max into Cells(Count, 4)

这将返回 max 到 Cells(Count, 4)