从条件格式确定单元格颜色的更改

时间:2017-04-27 12:23:37

标签: excel vba excel-vba colors conditional-formatting

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

fig.draw

2 个答案:

答案 0 :(得分:2)

您需要使用单元格的.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)中使用。

答案 1 :(得分:0)

感谢您的帮助!你救了我很多时间。这就是我最后使用encase任何人关心。我最终注意到最后一个字是完全所以我可以使用正确的功能

Sub Macro6()

lastrow = Sheet4.Cells(Sheet4.Rows.Count, "A").End(xlUp).Row
Dim A As Integer
For N = 1 To  lastrow

A = 1
If Right(Sheet4.Cells(N, 1), 7) = "Totals:" Then
' MsgBox ("Found at" & Sheet4.Cells(N, 1))
'MsgBox (N)
Sheet4.Cells(N, 1).Interior.Color = 13551615

Else
'nothing
' MsgBox ("Found at" & Sheet4.Cells(N, 1))
' MsgBox (N)
End If
'A = A + 1
Next


End Sub
相关问题