打印电子表格的所有条件格式规则

时间:2016-08-11 06:47:33

标签: excel-vba vba excel

我希望VBA代码在电子表格中打印每个条件格式规则,包括规则适用的规则类型,规则说明(公式),颜色和单元格范围。 我如何实现这一目标?

3 个答案:

答案 0 :(得分:6)

你可以很容易地将它列出来。

Sub ListAllCF()
    Dim cf As FormatCondition
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cf In ws.Cells.FormatConditions
        Debug.Print cf.AppliesTo.Address, cf.Type, cf.Formula1, cf.Interior.Color, cf.Font.Name
    Next cf
End Sub

但由于某些类型无法使用这种方式列出,因此您需要捕获错误并找到列出错误类型的其他方法。

答案 1 :(得分:3)

Rosetta的答案很好,但我认为了解运营商(大于,小于等)非常重要,所以我对其进行了修改:

Sub ListAllCF()
    Dim cf As FormatCondition
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cf In ws.Cells.FormatConditions
        Debug.Print cf.AppliesTo.Address, cf.Type, cf.Operator, cf.Formula1, cf.Interior.Color, cf.Font.Name
    Next cf
End Sub

答案 2 :(得分:1)

Rosetta的答案非常适合基于表达式的典型FormatConditions,但Excel支持该例程无法处理并导致错误的其他条件格式类型。这是更新的例程,列出了活动工作表上的所有条件。我没有列出每种类型的详细信息,但是您可以根据需要添加更多信息。请注意,cf.Operator属性仅存在于某些表达式上,因此我没有包含它。

使此代码起作用的主要区别是 cf 变量需要声明为 Object ,因为Cells.FormatConditions可以返回多种数据类型。

Public Sub ListAllCF()
' List all conditional formatting on the current sheet.  Use for troubleshooting.

    Dim cf As Object ' This can multiple types such as FormatCondition/UniqueValues/Top10/AboveAverage/...
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Debug.Print "Applies To", "Type", "Formula", , "Bold", "Int. Color", "StopIfTrue"
    For Each cf In ws.Cells.FormatConditions
        Debug.Print cf.AppliesTo.Address,
        Select Case cf.Type 'List of Types:  https://docs.microsoft.com/en-us/office/vba/api/excel.xlformatconditiontype
            Case 1, 2, xlExpression
                Debug.Print "Expression", cf.Formula1, cf.Font.Bold, cf.Interior.Color, cf.StopIfTrue
            Case 8, xlUniqueValues
                Debug.Print "UniqueValues", IIf(cf.DupeUnique = xlUnique, "xlUnique", "xlDuplicate"), , cf.Font.Bold, cf.Interior.Color, cf.StopIfTrue
            Case 5, xlTop10
                Debug.Print "Top10", IIf(cf.TopBottom = xlTop10Top, "Top ", "Bottom ") & cf.Rank & IIf(cf.Percent, "%", ""), , cf.Font.Bold, cf.Interior.Color, cf.StopIfTrue
            Case 12, xlAboveAverageCondition
                Debug.Print "AboveAverage", IIf(cf.AboveBelow = xlAboveAverage, "Above Average", IIf(cf.AboveBelow = xlBelowAverage, "Below Average", "Other Average")), , cf.Font.Bold, cf.Interior.Color, cf.StopIfTrue

            '--- Add your own code to support what you want to see for other types.
            Case 3, xlColorScale
                Debug.Print "ColorScale..."
            Case 4, xlDatabar
                Debug.Print "Databar..."
            Case Else
                Debug.Print "Other Type=" & cf.Type

        End Select
    Next cf
    Debug.Print ws.Cells.FormatConditions.count & " rules on " & ws.Name
End Sub

示例输出

Applies To    Type          Formula                     Bold          Int. Color    StopIfTrue
$A$1:$S$36    Expression    =CELL("Protect",A1)=0       Null           16777215     False
$B:$B         UniqueValues  xlDuplicate                 True           13551615     False
$H:$H         Top10         Top 10                      Null           13551615     False
$I:$I         Top10         Top 20%                     Null           13551615     False
$J:$J         Top10         Bottom 13%                  Null           13561798     False
$K:$K         AboveAverage  Above Average               Null           13551615     False
$L:$L         AboveAverage  Below Average               Null           10284031     False
$H:$H         ColorScale...
$I:$I         Databar...
$M:$M         Other Type=6
10 rules on Sheet1