【发布时间】: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<TODAY(),E2<=98,A2+14>TODAY())将新的条件格式规则添加到单元格 D2 并将格式复制到 D 中的其他单元格
标签: excel conditional conditional-formatting vba