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

D.P*_*son 4 excel vba excel-vba

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

Ben*_*Ben 10

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
Run Code Online (Sandbox Code Playgroud)

示例输出

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
Run Code Online (Sandbox Code Playgroud)


Ros*_*tta 7

你可以很容易地列出这样的.

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
Run Code Online (Sandbox Code Playgroud)

但它的错误,因为某些类型不能使用这种方式列表,所以你需要捕获错误并找到列出错误类型的其他方法.