[英]How to Loop through a column in VBA with Muiltiple columns?
我正在尝试突出显示Excel VBA中的到期日期。 我需要B列突出显示将在下个月内到期的日期,C列突出显示将在4到6个月内到期的日期。 目前,如果我删除列并一次在一个列上运行代码,则该代码有效
Name Green Will Expire within 1 Month
Name 1 01/01/2013
Name 2 17/07/2013
Name 3 03/04/2013
Name 4 24/03/2013
Name 5 16/07/2013
Name 6 26/01/2013
Name 7 28/06/2013
Name 8 01/07/2013
Name 9 09/01/2013
Name 10 31/07/2013
名称(A列),绿色将在1个月内过期(B列)。
如果仅使用这两列运行代码(已删除列C),则代码可以正常工作。 如果我在第三列中包括“橙色在4到6个月内到期”(列C):
Name Green Will Expire within 1 Month Orange Expires in 4 - 6 Months
Name 1 01/01/2013 01/01/2013
Name 2 17/07/2013 01/12/2013
Name 3 03/04/2013 03/04/2013
Name 4 24/03/2013 20/11/2013
Name 5 16/07/2013 16/07/2013
Name 6 26/01/2013 26/01/2013
Name 7 28/06/2013 28/06/2013
Name 8 01/07/2013 01/07/2013
Name 9 09/01/2013 09/01/2013
Name 10 31/07/2013 31/07/2013
仅第二个for循环有效。 我需要运行这两个for循环而不必删除任何列。
VBA代码:
Private Sub CommandButton1_Click()
Dim Cell As Range
For Each Cell In Range("B2:B1000").Cells
If Cell.Value <= Date + 30 And Not (Cell.Value < Date) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 35
End If
Next Cell
For Each Cell In Range("C2:C1000").Cells
If Cell.Value <= Date + 180 And Not (Cell.Value <= Date + 120) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 45
End If
Next Cell
End Sub
任何帮助是极大的赞赏。
谢谢
如果列C不为空,则条件语句的一部分将始终评估为False
:
And IsEmpty(Cell.Offset(0, 1).Value)
值得一提的是,同一片段在第二个循环中,因此您可能也希望将其从那里删除。 但是我不确定这部分是否需要逻辑。 无论如何,这都是成立的:如果D列不为空,则它将始终为false,因此If...Then
的主体将被省略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.