vba - 从条件格式确定单元格颜色的变化

标签 vba excel colors conditional-formatting

所以我的目标是更改任何包含“Totals:”字样的字符串的颜色。然后使用逻辑突出显示单元格 B1 和 C1。我当前的代码可以更改颜色,但是当我使用逻辑时,它认为单元格没有被填充。

Sub Macro6()
'
' Macro6 Macro
'


   'ActiveWindow.SmallScroll Down:=-12
Range("A1:A381").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="Totals:", 
TextOperator:=xlContains
'  


Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
'  With Selection.FormatConditions(1).Font
     '     .Color = -16383844
    '    .TintAndShade = 0
  '  End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13551615 '16777215 '
    .TintAndShade = 0
 End With
'Selection.FormatConditions(1).StopIfTrue = False
'lastrow = Sheet4.Cells(Sheet4.Rows.Count, "A").End(xlUp).Row



If Sheet1.Cells(9, 1).Interior.Color = 13551615 Then '16777215
MsgBox ("yes")
Else
MsgBox ("wrong")
MsgBox (Sheet4.Cells(6, 1).Interior.Color)
End If






End Sub

最佳答案

您需要使用单元格的 .DisplayFormat 属性来检索条件格式规则的格式方面。

Option Explicit

Sub Macro1()
    With Worksheets("Sheet1")
        With .Range("A1:A381")
            .FormatConditions.Delete
            With .FormatConditions.Add(Type:=xlTextString, String:="Totals:", TextOperator:=xlContains)
                .Interior.Color = 13551615 '16777215
                .SetFirstPriority
                .StopIfTrue = False
            End With
        End With
    End With

    With Worksheets("Sheet1")
        MsgBox CBool(.Range("A9").DisplayFormat.Interior.Color = 13551615)
    End With
End Sub

值得注意的是,.DisplayFormat 不能在用户定义的函数(又名 UDF)中使用。

关于vba - 从条件格式确定单元格颜色的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657732/

相关文章:

Excel VBA : Object doesn't support this property or method

excel - 如何根据特定日期更改行的颜色?

excel - 关于 Dim 和 Set Worksheet 的一些疑问

android - libgdx - 如何使用动画效果更改我的字体颜色

excel - 一种更准确、更高效的模糊搜索算法

ms-access - MS Access 2007 - 循环浏览列表框中的值以获取 SQL 语句的 id

excel - 按多个标准和从列中的最新日期排序/查找数据

javascript - 贪吃蛇游戏随机颜色

javascript - 转换颜色十六进制值以在 Three.js json 文件中使用

excel - 将所有 Excel 工作表设置为定义的缩放级别