【问题标题】:print all conditional formatting rules for a spreadsheet打印电子表格的所有条件格式规则
【发布时间】:2016-08-11 06:47:33
【问题描述】:

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

【问题讨论】:

  • 您好,所以不会为您编写代码。请分享您迄今为止尝试过的代码,让我们知道您遇到的问题。
  • 您可以修改此示例以满足您的需要Shoe Formatting Conditions。如果您在修改它时遇到问题,请发布您的代码,我们很乐意提供帮助。
  • 非常感谢,我什至不知道从哪里开始。节目格式条件正是我所需要的。

标签: excel vba


【解决方案1】:

你可以这样列出它,很容易。

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

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

【讨论】:

  • 正是我需要的,tks。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    Rosetta 的回答不错,但是我觉得了解 Operator 很重要(大于、小于等),所以我修改了一下:

    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
    

    【讨论】:

    • 我在cf.Operator 上收到了Run-time error '1004' / Application-defined or object-defined error。 (Excel 365)。我的测试条件只是=$A1="a"
    • 好吧,@Ben 的回答是“请注意,cf.Operator 属性仅存在于某些表达式中......”。我不知道。
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多