基于日期字符串的条件格式

时间:2020-07-03 12:04:31

标签: excel date conditional-statements instr

在我的表格中,日期的格式为“ dddd,mmmm dd,yyyy”。例如“ 2020年7月5日,星期日”。我希望宏格式化包含单词“ Sunday”的单元格,并且无法使其正常工作。我可以将其更改为dd字符串(即17)或yyyy字符串(即2020),并且可以使用,但不能更改包含文本的dddd或mmmm字符串。

Sub colour()
Dim rng As range
Dim lastRow As Long
Dim cell As range
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = range("J2:J" & lastRow)
For Each cell In rng
'If I change "Sunday" to "17" for example, or "2020", the routine works, but I cannot get it to find 
'the dddd string
If InStr(cell.Value, "Sunday") > 0 Then
range(cell.Address).Interior.ColorIndex = 19
Else
range(cell.Address).Interior.ColorIndex = 0
End If
Next cell
End Sub

1 个答案:

答案 0 :(得分:0)

斯蒂芬

您可以直接在工作表上执行此操作: enter image description here

或通过代码:

Option Explicit

Sub HighlightSundays()

   Dim rng As Range

   Set rng = Range("A1:A21")
   
      With rng
          .FormatConditions.Add Type:=xlExpression, _
           Formula1:="=WEEKDAY(A1)=7"
          .FormatConditions(.FormatConditions.Count).SetFirstPriority

        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
           .TintAndShade = 0
        End With
        
        .FormatConditions(1).StopIfTrue = True
        
     End With
      
End Sub

HTH