【发布时间】:2019-11-16 02:07:56
【问题描述】:
我有由条件格式 GREATER THAN / LESSER THAN 格式化的列。我想要一个函数来获取范围和引用的单元格。
我从网上尝试了几个 UDF,它们要么不计算任何颜色,要么计算每个有条件的彩色单元格,而不是应该计算其颜色的引用单元格。例如:
Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range)
Dim Bambo As Boolean
Dim dbw As String
Dim CFCELL As Range
Dim CF1 As Single
Dim CF2 As Double
Dim CF3 As Long
Bambo = False
For CF1 = 1 To CellsRange.FormatConditions.Count
If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
Bambo = True
Exit For
End If
Next CF1
CF2 = 0
CF3 = 0
If Bambo = True Then
For Each CFCELL In CellsRange
dbw = CFCELL.FormatConditions(CF1).Formula1
dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
If Evaluate(dbw) = True Then CF2 = CF2 + 1
CF3 = CF3 + 1
Next CFCELL
Else
COUNTConditionColorCells = "NO-COLOR"
Exit Function
End If
COUNTConditionColorCells = CF2
End Function
这个 UDF 给了我 NO-COLOR。我认为无法检测到颜色,但我还能做什么?有什么想法吗?
【问题讨论】:
-
不要使用 UDF,而是使用具有用于条件格式的逻辑的公式。
-
请缩进您的代码,使其更具可读性和更易于调试。
-
Rubberduck 有一个缩进来帮助你
标签: excel vba conditional-formatting