使用Cells(x,y)作为输入选择max value excel

时间:2013-06-19 18:15:25

标签: excel-vba max vba excel

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

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

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

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

使用该代码我一直收到错误"预期:列表分隔符或)"

编辑,这是整个代码

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

1 个答案:

答案 0 :(得分:0)

您可以使用Max上的Application.WorksheetFunction功能,Range使用Cells(m, 1)Cells(n, 1)

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

这会将最大值返回Cells(Count, 4)