【问题标题】:Excel VBA: Coloring cells under multiple conditionsExcel VBA:在多种条件下为单元格着色
【发布时间】:2018-07-03 11:00:03
【问题描述】:

我目前正在修改一些预先编写的代码,所以我创建了一个测试虚拟文件。我正在经历不当行为,我无法找到罪魁祸首。首先是我的 Excel 数据表示例:

Issue Date  Maturity    Status  ISIN            Price
19/01/2018  06/01/2020  Issued  XS2375645421    97
25/01/2013  01/01/2020  Issued  XS0879579182    88
12/01/2015  07/01/2020  Issued  XS4158674165    92
20/01/2018  05/01/2020  Issued  XS5458614653    98
31/01/2018  03/01/2020  Traded  XS5445656466    87
06/02/2018  02/01/2020  In Sub  XS1515113535    99

此外,您会在下面找到我的代码:

Sub Button1_Click()


Dim wb As Workbook
Dim ws As Worksheet
Dim count As Integer
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")
'if wb is other than the active workbook
wb.Activate
ws.Select


'Colorizing The ISIN with the following 3 conditions:

'1.) Issue Date <= today
'2.) Issue Date + 14d > today
'3.) Price <= 98
'So in summary the conditions mean that today has to be in between the Issue Date
'and 14 days after the Issue Date and the price has to be lower than 98

count = 0
Do While CDate(ws.Cells(2 + count, 1).Value) <= CDate(Now()) And _
ws.Cells(2 + count, 5).Value <= 98 And _
CDate(DateAdd("d", 14, ws.Cells(2 + count, 1).Value)) > CDate(Now())

count = count + 1

ws.Range("D" & count + 1).Interior.Color = RGB(250, 50, 50)

Loop



End Sub

代码部分工作,第一个 ISIN 值被着色,但之后如果不满足所有条件,循环会突然停止。如果继续,第 5 行中的 ISIN 也应该是彩色的,因为所有条件都满足。请看下面的截图:

谁能帮我解决这个问题?

提前致谢!

亲切的问候

【问题讨论】:

  • 它会停止,因为这就是您设置循环的方式。为什么不使用 For 循环?
  • 你为什么不使用条件格式呢?无需使用 VBA。使用此公式=AND(A2&lt;TODAY(),E2&lt;=98,A2+14&gt;TODAY()) 将新的条件格式规则添加到单元格 D2 并将格式复制到 D 中的其他单元格

标签: excel conditional conditional-formatting vba


【解决方案1】:

问题

您的循环停止,因为它只运行到 3 个条件中的 一个 为假。还有你的情况

CDate(DateAdd("d", 14, ws.Cells(2 + count, 1).Value)) > CDate(Now())

对于第二个数据行已经是假的了。这意味着跳过第二行之后的所有内容。


VBA 解决方案

因此,您需要一个循环遍历所有数据行,并使用if 语句检查条件是否满足。如果为 true,则为它着色,如果不移动到下一行。

Public Sub Button1_Click()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim count As Long 'we need to use Long instead of Integer
                      'Excel has more rows than Integer can handle
    Set wb = ThisWorkbook 'ThisWorkbook = the wb where this code runs .. is better than
                          'ActiveWorkbook = any workbook that is in focus at the moment
    Set ws = wb.Sheets("Sheet1")
    'if wb is other than the active workbook
    wb.Activate 'this is not needed to run the code
    ws.Select 'this is not needed to run the code

    count = 0
    Do While ws.Cells(2 + count, 1).Value <> vbNullString 'do while first cell contains data
        If CDate(ws.Cells(2 + count, 1).Value) <= CDate(Now()) And _
           ws.Cells(2 + count, 5).Value <= 98 And _
           CDate(DateAdd("d", 14, ws.Cells(2 + count, 1).Value)) > CDate(Now()) Then

            'color it
            ws.Range("D" & count + 1).Interior.Color = RGB(250, 50, 50)
        End If

        count = count + 1 'next row
    Loop
End Sub

注意:看看我制作的改进代码的 cmets。


条件格式解决方案

作为 VBA 的替代方案,我建议使用条件格式。

使用此公式添加新的条件格式设置规则

=AND(A2<TODAY(),E2<=98,A2+14>TODAY())

到单元格 D2 并将格式复制到 D 列中的其他单元格。 当您更改数据值时,条件格式会立即更改,您无需为此运行 VBA 代码。

【讨论】:

  • 工作就像一个魅力非常感谢你,这真的很有帮助。但是它也会为列标题(ISIN)着色,我该如何摆脱它?
  • 没关系,找到问题了,没有删除我以前的着色,对不起..感谢您的解决方案,非常感谢!
猜你喜欢
  • 2012-10-15
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
相关资源
最近更新 更多