如何通过多列的VBA中的列循环?

我想在Excel VBA中突出显示有效date。 我需要列B来突出显示将在下个月内过期的date和列c以突出显示将在4 – 6个月内过期的date。 目前代码的作品,如果我删除列和一次运行一列的代码,例如

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的主体将被省略。