在 VBA 中用文本值替换公式输出

In VBA replace formula output with text value

我有 VBA 代码使我的列 WEEKNUM 公式在特定周数范围内显示 "Late"。当代码为运行时,公式将周数变成单词"Late";但是,这会转到 sheet 上方的枢轴 table,并且当 table 刷新时 "Late" 值不会显示,因为它仍然处于公式。有没有一种方法可以在 WEEKNUM 值为 "Late" 时清除单元格并输入文本 "Late"?更好的是可以将公式输出替换为文本值吗?

    With MyWorkbook.Worksheets("sheet2")
    With .Range(.Cells(2, "X"), .Cells(.Rows.Count, "O").End(xlUp).Offset(0, 9))
        .Formula = "=weeknum(o2)"
        .NumberFormat = "0_)"
        .FormatConditions.delete
        With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and(x2<weeknum(today()), year(o2)=year(today()))")
            .NumberFormat = "\L\a\t\e_)"
        End With
    End With
End With

这将给出 "Late" 作为公式的结果而不是部分格式。由于数据透视表通常将公式结果文本视为 "just" 文本,因此这应该会给出所需的结果。

替换:

With .Range(.Cells(2, "X"), .Cells(.Rows.Count, "O").End(xlUp).Offset(0, 9))
    .Formula = "=weeknum(o2)"
    .NumberFormat = "0_)"
    .FormatConditions.Delete
    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and(x2<weeknum(today()), year(o2)=year(today()))")
        .NumberFormat = "\L\a\t\e_)"
    End With
End With

有了这个:

With .Range(.Cells(2, "X"), .Cells(.Rows.Count, "O").End(xlUp).Offset(0, 9))
    .Formula = "=if(and(weeknum(o2)<weeknum(today()), year(o2)=year(today())),""Late"",weeknum(o2))"
    .NumberFormat = "0_)"
End With