2014-04-13 56 views
-1

我有2列,ColumnA包含日期(例如3/15/2014)和ColumnB的公式为ColumnA+30(例如=$A1+30)。Excel VBA为过期日期添加条件格式

我需要做的是通过VBA添加一个条件格式,其中ColumnB如果它的值(这是一个日期)小于今天的日期,它将变为红色。

基本上ColumnA是用于“制造日期”和ColumnB是“过期日期”,应该是制造后30天。目标是在ColumnB已经过期的情况下将红色单元格变成红色。条件格式必须通过VBA代码添加。

我试图录制一个宏,但结果很糟糕。

Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Count).FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _ 
    Formula1:="=""Today()""" 
Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Count).FormatConditions(Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Count).FormatConditions.Count).SetFirstPriority 
With Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Count).FormatConditions(1).Interior 
    .PatternColorIndex = xlAutomatic 
    .Color = 255 
    .TintAndShade = 0 
End With 
Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Count).FormatConditions(1).StopIfTrue = False 
+0

告诉我们请将当前的代码 –

+0

我的代码可能很难理解,但你去那里,我重视它。它不完全是ColumnA和ColumnB,而是一个更复杂的例子。 – Jay

回答

0

试试这个:

Sub test() 
    'change Sheet2 to suit 
    With ThisWorkbook.Worksheets("Sheet2").Range("B:B").FormatConditions 
     .Add Type:=xlExpression, Formula1:="=AND(B1<>"""",B1<TODAY())" 
     With .Item(.Count) 
      .Interior.Color = 255 
      .SetFirstPriority 
     End With 
    End With 
End Sub