2016-07-14 144 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