【问题标题】:VBA If function with date and text带有日期和文本的 VBA If 函数
【发布时间】:2021-03-09 08:36:58
【问题描述】:
Sub SetFormulasFormat()
 
Application.ScreenUpdating = False
Dim cl As Range

With ActiveSheet      
For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
    If UCase(cl.Value) = "1ST" Then

我想检查 C 列中的日期是否是工作日,并在 if 中使用它,但似乎没有任何效果。 我试过了

If UCase(cl.Value) = "1ST" AND Weekday(cl.Offset(, 2).Value, vbMonday) <6 

然后在这里我得到一个错误

有人可以帮帮我吗? :-)

【问题讨论】:

  • 使用Weekday(cl.Offset(, 2), vbMonday) &lt; 6 Then
  • 然后我得到一个类型不匹配的错误。
  • 如果cl指的是E列,而你的日期在C列中,也许你想要Weekday(cl.Offset(, -2).Value)
  • 仍然出现同样的错误。
  • 那么您的日期可能不是真实日期。或者可能是与您的数据有关的其他错误

标签: excel vba date


【解决方案1】:

所以这是我的文件。我一周中的所有日子都在一张纸上。

我要做的是在 1ST、CUCA、SERVICE ADMIN、2ND、CHAT 的行中使用条件格式。我已经想通了,但现在我希望这仅适用于周一至周四,然后适用于周五、周六、周日的其他规则。

这就是我目前拥有的代码

Sub SetFormulasFormat()

Application.ScreenUpdating = False 暗淡作为范围 使用 ActiveSheet

For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
  
    If UCase(cl.Value) = "1ST" And Weekday(cl.Offset(, -2), vbMonday) < 6 Then
        .Range("F" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 1 single cell in same row
        .Range("F" & cl.Row).FormatConditions(1).Interior.Color = vbRed
        
        .Range("G" & cl.Row & ",H" & cl.Row & ",Y" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=3" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("G" & cl.Row & ",H" & cl.Row & ",Y" & cl.Row).FormatConditions(1).Interior.Color = vbRed
        
        .Range("I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=4" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions(1).Interior.Color = vbRed

        .Range("I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=7" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row).FormatConditions(2).Interior.Color = RGB(128, 0, 128)
        
        .Range("J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=5" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row).FormatConditions(3).Interior.Color = RGB(255, 140, 0)
        
        .Range("Z" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=2" 'apply CF rule to 1 single cell in same row
        .Range("Z" & cl.Row).FormatConditions(1).Interior.Color = vbRed
        
    End If
    If UCase(cl.Value) = "CUCA" Then
        
        .Range("G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions(1).Interior.Color = vbRed

    End If
    If UCase(cl.Value) = "SERVICE ADMIN" Then
        
        .Range("G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions(1).Interior.Color = vbRed
    End If
    
    If UCase(cl.Value) = "2ND" Then
        
        .Range("F" & cl.Row & ",G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("F" & cl.Row & ",G" & cl.Row & ",H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row & ",X" & cl.Row).FormatConditions(1).Interior.Color = vbRed
    
     End If
     
    If UCase(cl.Value) = "CHAT" Then
        
        .Range("H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
        .Range("H" & cl.Row & ",I" & cl.Row & ",J" & cl.Row & ",K" & cl.Row & ",L" & cl.Row & ",M" & cl.Row & ",N" & cl.Row & ",O" & cl.Row & ",P" & cl.Row & ",Q" & cl.Row & ",R" & cl.Row & ",S" & cl.Row & ",T" & cl.Row & ",U" & cl.Row & ",V" & cl.Row & ",W" & cl.Row).FormatConditions(1).Interior.Color = vbRed
                                 
    
    End If
    
Next cl

结束于 Application.ScreenUpdating = True 结束子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多