[英]If cell is highlight then
我有一个表格,我想计算一行中突出显示的单元格数量?
我试过寻找一个公式。 我尝试针对彩色单元格获取 COUNT-IF。
我猜你需要某种 VBA 代码,因为没有公式(否则我不会在这里)
任何选项来获取突出显示的单元格计数?
我不认为有像 vlookup、sum、countif 等直接的公式。 所以不幸的是它需要一个宏。
创建一个宏,这是我使用的代码:
Sub COUNT_HIGHLIGHTS()
'
' COUNT_HIGHLIGHTS
'
'Defining variables
Dim LastRow As Long, Count As Integer
'Getting a number value for the number of rows in the table (as this can vary on table size)
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
'For loop to loop through rows
For i = 2 To LastRow
'Count is the number of cells in the row which are highlighted
Count = 0
'For loop for cells within the row (My table always has 36 cells)
For j = 5 To 36
'If statement to check if cell is highlighted
If Cells(i, j).Interior.Color = 65535 Then
'Add +1 to count ever time a highlighted cell is found
Count = Count + 1
End If
Next j
'find cell at the end of the row and add the count
Cells(i, 37).Select
Selection.Value = Count
Next i
End Sub
完成后,您会在最后获得一列
第 1 列 | 第 2 栏 | 第 3 栏 | ...... 表格外的列 ColValue 1 | ColValue2| ColValue3| ....... 5(如果表中的 5 个单元格高亮显示)
然后只需将表过滤到您创建的计数值,除 0 之外的所有内容
希望这有帮助
您可能喜欢为此使用 UDF(用户定义函数)。 这是一个。 它将计算所有并非没有颜色的单元格,无论它们具有哪种颜色。
Function HighLightCount(Target As Range) As Integer
' 14 Feb 2018
Dim Cell As Range
Dim Fun As Integer
For Each Cell In Target
Fun = Fun - Int(Cell.Interior.Color <> 16777215)
Next Cell
HighLightCount = Fun
End Function
将代码安装在标准代码模块中(默认情况下,在您更改名称之前,它将是Module1
)。 像普通 Excel 函数一样从工作表中调用该函数,例如,
=HighLightCount(A2:J2)
您可以像其他任何单元格一样将公式复制/粘贴到其他单元格。 您还可以将其嵌入到 Excel 的内置函数中,例如,
=IF(HighLightCount(A6:J6)>2,SUM(A6:D6),SUM(E6:J6))
如果您想按特定颜色搜索单元格,以下 UDF 将为您提供帮助。
Function CountCellsByColor(SearchRange As Range, RefCellColor As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long
Application.Volatile
cntRes = 0
indRefColor = RefCellColor.Cells(1, 1).Interior.Color
For Each cellCurrent In SearchRange
If indRefColor = cellCurrent.Interior.Color Then
cntRes = cntRes + 1
End If
Next cellCurrent
CountCellsByColor = cntRes
End Function
SearchRange
是您要搜索的范围,而RefCellColor
是您在SearchRange
中查找的颜色格式的参考单元格。
有一种方法可以做到这一点没有VBA -但它是非常混乱的,你都需要一个“工作”行,并在命名范围的Excel4宏。 我只是为了完整性而对其进行详细说明。
第 1 步:创建一个名为“Background_Below”的命名范围(FORMULAS > NAME MANAGER)并在下面引用: =GET.CELL(63,INDIRECT("r[-1]c",FALSE))
第 2 步:在要计算突出显示单元格的行下方的行上添加一个工作行,并将公式=Background_Below
放在该行中。 这将是“无颜色”的 0 或“颜色”的 >0(实际值将是与上面单元格颜色最接近的xlColorIndex
值)
第 3 步:在工作行 >0 的地方做一个COUNTIF
正如我所说,它很混乱 - 至少有 9 次在 VBA 中使用用户定义函数会更好。 但这是可能的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.