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

标签 excel vba replace pivot-table week-number

我有 VBA 代码让我的列 WEEKNUM 公式说“迟到”,如果它在某个周数范围内。该公式在代码运行时将周数变成单词“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

最佳答案

这将给出“迟到”作为公式的结果,而不是格式的一部分。由于数据透视表通常将公式结果文本视为“只是”文本,因此这应该会产生所需的结果。

代替:

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

关于excel - 在 VBA 中,用文本值替换公式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51289831/

相关文章:

vba - 使用 VBA 从字符串中提取连续数字

Javascript,替换为正则表达式,这种情况可以吗?

excel - Module1 中的 VBA 组合框值

eclipse - 在 Eclipse 中,我可以将“使用就地编辑器打开”设置为默认选项吗?

excel - 如何在Excel中对日期进行排名而不重复

string - 从字符串中删除给定字符(D语言)

javascript - 正则表达式中的文字美元符号

javascript - 提高创建 Excel 工作表的速度(以编程方式)

vba - Excel VBA "Autofill Method of Range Class Failed"

excel - 将 Visual Basic 编辑器的语言更改为英语