VBA无法找到并替换日期

时间:2018-01-05 16:45:24

标签: excel vba excel-vba

我在A1有个约会日期:

enter image description here

我想像这样更改单元格:

enter image description here

所以我录制了宏但它没有做任何事情

Sub Macro1()

    With Application.ReplaceFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Cells.Replace What:="08/01/2018", Replacement:="09/01/2018", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=True
End Sub

如何让它发挥作用?

谢谢!

2 个答案:

答案 0 :(得分:4)

您正在尝试使用字符串进行搜索/替换,但实际上您在单元格中有一个日期(即数字)。

Sub Macro1()

    With Application.ReplaceFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Cells.Replace What:=CDate("08/01/2018"), Replacement:=CDate("09/01/2018"), LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=True
End Sub

答案 1 :(得分:2)

这似乎适用于我的机器..

With Application.ReplaceFormat.Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Cells.Replace What:=DateValue("08/01/2018"), Replacement:=DateValue("08/01/2018"), LookAt:= _
    xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=True