【问题标题】:Deterine a change in cell color from conditional formatting从条件格式确定单元格颜色的变化
【发布时间】:2017-09-25 05:54:01
【问题描述】:

所以我的目标是更改任何包含单词“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

【问题讨论】:

  • 使用格式化.....

标签: vba excel colors conditional-formatting


【解决方案1】:

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

【讨论】:

  • 好的,因为我不能将显示格式用作函数,我可以使用 worksheetfunction.find("totals:") 吗?我希望的最终结果是能够识别细胞,然后执行一个功能。哪个运营商最适合这个?
  • 我应该注意每一行都是不同的。但它们都有共同的单词总数:
  • 您可以对 Totals: 或 CFR 颜色进行 .AutoFilter。
【解决方案2】:

感谢您的帮助!你为我节省了很多时间。这就是我最终使用的封装任何人关心的内容。我最终注意到最后一个词是完全的,所以我可以使用正确的功能

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多