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

问题描述 投票:1回答:3

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

excel-vba vba excel
3个回答
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

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


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

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
© www.soinside.com 2019 - 2024. All rights reserved.