2016-07-14 143 views
0

我在Excel中创建日历/日程安排工作簿。 所有星期的日子都是蓝色的,周末的日子都是红色的。Excel - 如何根据细胞的颜色和相邻细胞的颜色来计数细胞?

我需要做的一件事是计算某个人工作多少个工作日(bue细胞),但没有星期五(也是蓝色细胞,但是在红色细胞(星期六)之上)。

计数的工作日总数(蓝色细胞)一个人的工作是没有问题的:

Function CountCcolor(range_data As Range, criteria1 As Range, criteria2 As Range) As Long 
    Dim datax As Range 
    Dim xcolor As Long 
xcolor = criteria1.Interior.ColorIndex 
radiologist = criteria2.Value 
For Each datax In range_data 
    If datax.Interior.ColorIndex = xcolor And datax.Value = radiologist Then CountCcolor = CountCcolor + 1 
    End If 
Next datax 
End Function 

这包括周五虽然,我不想要的。

是否可以扩展此代码以排除所有正好在红色单元之上的蓝色单元格?

+0

为什么不使用Weekday()测试基础日期,如果它是周末或周末。 –

+0

如果它必须基于颜色,你可以在你的if语句中添加'和datax.offset(-1,0)<> ycolour'来查看上面的单元格。 – gtwebb

回答

0

使用Range.Offset property检查直接位于循环中的单元之下的单元。

For Each datax In range_data 
    If datax.Interior.ColorIndex = xcolor And _ 
     datax.OFFSET(1, 0).Interior.ColorIndex = xcolor And _ 
     datax.Value = radiologist Then 
     CountCcolor = CountCcolor + 1 
    End If 
Next datax 
+0

谢谢,完美的作品! – ExReey