0

在我的表格中,日期格式为“dddd,mmmm dd,yyyy”。例如“2020 年 7 月 5 日,星期日”。我希望宏格式化包含“星期日”一词但无法正常工作的单元格。我可以将其更改为 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
4

1 回答 1

0

斯蒂芬,

您可以直接在工作表上执行此操作: 在此处输入图像描述

或通过代码:

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

高温高压

于 2020-07-03T12:59:57.933 回答