R
Rick Rothstein \(MVP - VB\)
Am I missing something... doesn't this do what the OP wanted?
=SUMPRODUCT(MAX(A1:A98+A2:A99+A3:A100))
Rick
=SUMPRODUCT(MAX(A1:A98+A2:A99+A3:A100))
Rick
Or if you like array formulas:
=MAX(A1:A98+A2:A99+A3:A100)
as an array formula (ctrl-shift-enter)
....Don Guillett said:Would it follow that the macro approach or turning into a UDF be
better than helper or array formula? ....
=sumthree("I")
Function sumthree(mc) 'UDF
For i = 2 To 100
mysum = Application.Sum(Range(Cells(i, mc), Cells(i + 2, mc)))
If mysum > highest Then highest = mysum
Next i
sumthree = highest
End Function ....
Harlan Grove said:...Don Guillett said:Would it follow that the macro approach or turning into a UDF be
better than helper or array formula? ...
=sumthree("I")
Function sumthree(mc) 'UDF
For i = 2 To 100
mysum = Application.Sum(Range(Cells(i, mc), Cells(i + 2, mc)))
If mysum > highest Then highest = mysum
Next i
sumthree = highest
End Function ...
Charles did say 'when properly written'.
udfs are always more flexible than macros, and they're subject to
automatic recalculation whereas macros must be run.
As for 'properly written', wouldn't it be more efficient to load
the .Value property of the range argument into a VBA array and use
that array rather than repeatedly accessing ranges? Also, wouldn't VBA
+ operations be faster than Application[.WorksheetFunction].Sum? IOW,
Function maxsumn(rng As Range, n As Long) As Variant
Dim sumn As Double, v As Variant, nv As Long, k As Long
If rng.Columns.Count > 1 Then 'no 2D ranges
maxsumn = CVErr(xlErrRef)
Exit Function
ElseIf rng.Rows.Count <= n Then 'return degenerate results fast
maxsumn = Application.WorksheetFunction.Sum(rng)
Exit Function
End If
v = rng.Value
nv = rng.Cells.Count - n
For k = 1 To n
sumn = sumn + v(k, 1)
Next k
maxsumn = sumn
For k = 1 To nv
sumn = sumn - v(k, 1) + v(k + n, 1)
If sumn > maxsumn Then maxsumn = sumn
Next k
End Function
....Charles Williams said:Timings are very dependent on the number of cells in the range:
(prob gives #num for 2000 cells, presumably overflow?)
What would the time be for the array formula
=MAX(A1:A98+A2:A99+A3:A100)