Finding the longest sequence of 0's in a row
Jamie,
There is no easy way to do that with one formula - but a helper row of formulas would make it easy.
For example, if you have values in row 9, starting in column B, then in B8 use the formula
=IF(AND(C9=0,B9=0),A8+1,0)
and copy to the right to match the values in B9:???9, then use
=MAX(8:8)
to determine the largest sequence of zero values.
Certainly a User-Defined-Function (VBA) is much neater: Insert the code below into a codemodule, and
use the function like this
=MaxSeq(9:9,0)
or
=MaxSeq(B:B,0)
or
=MaxSeq(A1:Z1,0)
HTH,
Bernie
MS Excel MVP
Function MaxSeq(r As Range, v As Double) As Variant
Dim i As Long
Dim c As Range
Dim CurCnt As Integer
Set r = Intersect(r, Application.Caller.Parent.UsedRange)
If r.Rows.Count 1 And r.Columns.Count 1 Then
MaxSeq = "Block"
Exit Function
End If
If Application.CountIf(r, v) = 0 Then
MaxSeq = 0
Exit Function
End If
For i = 2 To r.Cells.Count
If r(i).Value < "" And r(i - 1).Value < "" Then
If r(i).Value = v And r(i - 1).Value = v Then
CurCnt = CurCnt + 1
MaxSeq = Application.Max(MaxSeq, CurCnt)
Else
CurCnt = 0
End If
End If
Next i
MaxSeq = MaxSeq + 1
End Function
"MJW" wrote in message
...
Hi All,
Ok, I can't think of any remotely-easy way to do this. My data exists as
such: accounts listed in the rows, dates (by month) listed in the columns;
in the fields corresponding to each account/dates is the quantity of items
the account purchased in that month.
Is it formulaically possible (barring VBA) to have Excel calculate the
longest contiguous string of zero-value cells for each row? (What I'm
trying to discern is a historical gap-analysis for the longest no-purchase
period of a given row/account.) Any thoughts or suggestions are greatly
appreciated, as always.
Thanks,
Jamie W.
|