我正在尝试创建“旧”库存的热图。我已经创建了所有库存位置的地图,并且使用条件格式来突出显示每张纸上包含旧库存的单元格。有7张纸代表某个位置的每个级别。
我要准备一个隐藏的工作表,每个位置的红细胞计数(例如:在所有7个工作表上查看单元格C4
并保留红细胞计数)
我将参考这些计数来格式化位置概览表(绿色,黄色,橙色,红色)。
我正在使用以下VBA代码来尝试完成此操作:
Function ColorFunction(rColor As Range, rRange As Range, rRange2 As Range, _
rRange3 As Range, rRange4 As Range, rRange5 As Range, _
rRange6 As Range, rRange7 As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange2
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange3
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange4
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange5
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange6
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
For Each rCell In rRange7
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
下面的参考表3:
当我将相同的条件格式应用于同一张纸上的单元格(A1)并引用该颜色时,我看到了几个问题,我的计数显示为7,好像7种色都没有应用工作表上显示“无填充”。如果我将任何C4
单元格的颜色更改为其他颜色(白色,黄色,紫色),则数字将降至6,5,4 ....
我在A3
单元格中手动添加了红色,如果我将C4
单元格之一手动着色为红色,我将获得准确的计数。
关于如何解决此问题的任何建议?我已经验证了应用于所有7张纸的条件格式使用的是RGB(255,0,0),手动红色单元格也是RGB(255,0,0)。我无所适从。
答案 0 :(得分:0)
如果要通过条件格式检查颜色,则需要使用DisplayFormat.Interior.ColorIndex
。您当前的代码将仅检测静态颜色填充。
未经测试:
Function ColorFunction(rColor As Range, rRange As Range, rRange2 As Range, _
rRange3 As Range, rRange4 As Range, rRange5 As Range, _
rRange6 As Range, rRange7 As Range, Optional SUM As Boolean)
Dim rCell As Range, rng
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.DisplayFormat.Interior.ColorIndex = lCol Then
vResult = vResult + rCell.Value
End If
Next rCell
Else
For Each rng In Array(rRange, rRange2, rRange3, rRange4, _
rRange5, rRange6, rRange7)
For Each rCell In rng.Cells
If rCell.DisplayFormat.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
Next rng
End If
ColorFunction = vResult
End Function