首页 文章

Excel / VBA:如何将FormatCondition.NumberFormat设置为特殊的Variant值“Empty”?

提问于
浏览
0

当我在Excel中手动创建新的条件格式(使用标准对话框)时,将取消设置默认的NumberFormat . 这意味着:"Do not override the number format when applying a conditional format."当我在VBA调试器中查看条件格式对象时, FormatCondition.NumberFormat 具有特殊的 VariantEmpty .

当我通过 Range.FormatConditions.Add 使用VBA创建新的条件格式时,默认的NumberFormat是"General" . 使用VBA,当我尝试将 FormatCondition.NumberFormat 设置为特殊 VariantEmpty 时,代码运行(不会抛出错误),但结果是 ;; . 这与 Empty 非常不同 .

  • 这是否发生在更高版本的Excel中? (我无权尝试 . )

  • 这个问题有解决方法吗?

澄清一下:Excel范围(和单元格)可能有两种数字格式 . 第一个是基数格式,使用 Ctrl+1 设置 . 这不包括条件格式 . 第二种是条件格式数字格式 . 如果设置,并且触发格式条件,则第二个数字格式将覆盖基本数字格式 .

如果这很重要:我在Windows 7上使用Excel 2010(14.x) .

2 回答

  • 0

    那很有意思 . 但是,我注意到它实际上并没有改变我的数字的外观 . 在运行宏之前和之后,看到对常规的更改,我的普通数字,货币和日期看起来仍然相同 .

    另请注意,General的格式相当于清除“单元格格式”对话框中的“自定义”框 . 在VBA中它相当于:

    Activecell.NumberFormat = ""
    

    甚至:

    Activecell.NumberFormat = vbNullString
    

    换句话说,它很空 .

  • 0

    得到相同的结果,我试图将 FormatConditionFormatCondition 设置为与单元格数格式相同的值:

    MyCell.FormatConditions(1).NumberFormat = MyCell.NumberFormat
    

    它产生了一个错误,其结果是清空格式条件数字格式(正是我想要的)

    所以我终于尝试了这个,它解决了我的问题:

    On Error Resume Next
    MyCell.FormatConditions(1).NumberFormat = "this does not exist"
    Err.clear
    On Error GoTo 0   'or any error handling you already set
    

相关问题