VBA 如何在不更改其公式的情况下更改单元格的值(显示文本)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11429493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:30:06  来源:igfitidea点击:

How to VBA change cell's value (display text) without changing its formula?

excelvbaexcel-vbaformula

提问by Davuz

I've a problem with this VBA macro.

这个 VBA 宏有问题。

Sub demoChangeText()
    Application.Range("B2").Value = "Overidden text"
End Sub

My test is here. To run this macro, open it in Excel, press Ctrl+F8and choose demoChangeText.

我的测试在这里。要运行此宏,请在 Excel 中打开它,按Ctrl+F8并选择demoChangeText

As the result, this macro changes the value of cell B2(the text displayed to us) but clear its formula. I need to change B2's value BUT also need the formula to be remained.

结果,这个宏改变了单元格的值B2(显示给我们的文本)但清除了它的公式。我需要更改B2的值但也需要保留公式。

So my question is How to change the display text of cell without changing its formula?

所以我的问题是如何更改单元格的显示文本而不更改其公式?

UPDATE

更新

I ask this question because I'm trying to solve this problem

我问这个问题是因为我正在努力解决这个问题

回答by chris neilsen

I'm not sure if this will help, as it is a bit hard to tell what your underlying requirement is, but here goes anyway:

我不确定这是否会有所帮助,因为很难说出您的基本要求是什么,但无论如何:

Several things affect the displayof a cell:

有几个因素会影响单元格的显示

  1. the entered value, if its a constant
  2. the result of a calculation, if its a formula
  3. the format of the cell
  4. the conditional format(s) of the cell, if any
  1. 输入的值,如果它是一个常数
  2. 计算的结果,如果它是一个公式
  3. 单元格的格式
  4. 单元格的条件格式(如果有)

In the example sheet provided you have a formula of =ROW()&COLUMN()which returns a string result of 22

在提供的示例表中,您有一个公式,=ROW()&COLUMN()它返回一个字符串结果22

You can make this displaysomething else by applying a cell format,
eg a format of 0;0;0;Ov\e\r\ri\d\d\e\n t\extwill display anystring value as Overridden text

您可以通过应用单元格格式使其显示其他内容,
例如格式0;0;0;Ov\e\r\ri\d\d\e\n t\ext将显示任何字符串值Overridden text

This can by applied with VBA with

这可以通过与 VBA 一起应用

Range("B2").NumberFormat = "0;0;0;Ov\e\r\ri\d\d\e\n t\ext\s"

or

或者

Range("B2").NumberFormat = "0;0;0;""Overridden texts"""