【问题标题】:Coloring cells based on two conditions in vba根据vba中的两个条件为单元格着色
【发布时间】:2017-01-19 15:06:43
【问题描述】:

您好,如果满足两个条件,我需要将单元格的字体颜色更改为红色(M列):

  1. Col P 应该有文本“等待”并且
  2. Col M 的值应大于 1.0。

你能告诉我如何解决这个问题吗?

我尝试了下面的代码,但它不起作用:

Sub Latency()
    Dim r As Long
    Dim m As Long
    On Error GoTo ExitHere:
m = Range("A:B").Find(What:="*", SearchOrder:=xlByRows,  SearchDirection:=xlPrevious).Row
Application.ScreenUpdating = False
For r = 1 To m
    If Range("O" & r) = "*waiting* && Range("M" & r) >= 1 Then
        Range("M" & r).Cells.Font.ColorIndex = 3
    Else
        Range("M" & r).Cells.Font.ColorIndex = 0
    End If
Next r
ExitHere:
    Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 你遇到了什么错误?
  • If Range("O" & r) = "*waiting* && Range("M" & r) >= 1 Then 中的语法错误

标签: vba excel


【解决方案1】:

您的 m 已关闭,我拆分了您的 if 声明

编辑:颜色变回黑色是错误的,我更新了我的代码。

Sub Latency()
Dim r As Long
Dim m As Long
On Error GoTo ExitHere:
m = Range("M:O").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.ScreenUpdating = False
For r = 1 To m
    If Range("O" & r) = "waiting" Then
        If Range("M" & r) >= 1 Then
            Range("M" & r).Cells.Font.ColorIndex = 3
        End If
    Else
        Range("M" & r).Cells.Font.ColorIndex = 0
    End If
Next r
ExitHere:
Application.ScreenUpdating = True
End Sub

【讨论】:

  • 我试过这个,我没有得到任何语法错误也没有得到结果。在符合此条件的单元格中没有看到颜色:(
  • 在我的系统上工作正常...只是为了确定:值在 col "M" 和 "waiting" 在 col "O"
【解决方案2】:

查看代码的代码着色。您好像忘记在*waiting* 后面加上引号了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2014-05-04
    • 2023-04-07
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    相关资源
    最近更新 更多