简体   繁体   中英

How can I count cells in a row but only count adjacent cells once in Excel

Hi I have an spreadsheet with the following structure:-

电子表格示例

I need help to figure out a formula or VBA function, to calculate the Activity Block Count column.

This is the count of non blank, contiguous cells in the date columns. For example Project 5 has 4 weeks of activity but only 3 continuous blocks of activity.

Any idea warmly welcomed ;-). I'm working at the weekend and it's driving me insane!

Thanks

Jonathan

Just walk through the cells in the range, noting changes in whether the cell(s) are populated or not.

Function countActivityBlock(rng As Range) As Long

    Dim r As Range, bNum As Boolean

    If Not IsEmpty(rng.Cells(1)) Then
        bNum = True
        countActivityBlock = 1
    End If

    For Each r In rng
        If Not IsEmpty(r) And Not bNum Then
            bNum = Not bNum
            countActivityBlock = countActivityBlock + 1
        ElseIf IsEmpty(r) And bNum Then
            bNum = Not bNum
        End If
    Next r

End Function

在此处输入图片说明


SpecialCells(xlCelltypeConstants, xlNumbers) does not work within a UDF.

The formula way of doing this is using FREQUENCY as follows

=SUM(--(FREQUENCY(IF(D2:P2<>"",COLUMN(D2:P2)),IF(D2:P2="",COLUMN(D2:P2)))>0))

Must be entered as an array formula using Ctrl Shift Enter

See This and This

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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