Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA为过期日期添加条件格式_Vba_Excel - Fatal编程技术网

Excel VBA为过期日期添加条件格式

Excel VBA为过期日期添加条件格式,vba,excel,Vba,Excel,我有两列,列a包含一个日期(例如2014年3月15日),列B的公式为列a+30(例如=A1+30) 我需要做的是通过VBA添加一个条件格式,如果它的值(即日期)小于今天的日期,ColumnB单元格将变为红色 基本上,列A表示“制造日期”,而列B表示“到期日期”,应为制造后30天。我们的目标是在红细胞已经过期的情况下,将红细胞转入B列。必须通过VBA代码添加条件格式 我试着录制一个宏,但结果不好 Sheet2.Range("K2:L" & Sheet2.UsedRange.Rows.Cou

我有两列,列a包含一个日期(例如2014年3月15日),列B的公式为
列a+30
(例如
=A1+30

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

基本上,
列A
表示“制造日期”,而
列B
表示“到期日期”,应为制造后30天。我们的目标是在红细胞已经过期的情况下,将红细胞转入B列。必须通过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
试试这个:

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
子测试()
'将表2换成合适的
使用此工作簿。工作表(“Sheet2”)。范围(“B:B”)。格式条件

.Add Type:=xlExpression,Formula1:=“=和(B1)”,B1请向我们展示您当前的代码我的代码可能很难理解,但我已经附加了它。它不完全是ColumnA和ColumnB,而是一个更复杂的示例。