簡體   English   中英

從條件格式確定單元格顏色的更改

[英]Deterine a change in cell color from 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)中使用。

謝謝您的幫助! 你救了我很多時間。 這就是我最終使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM