如何更改单元格的值(显示文本)而不更改其公式? [英] How to VBA change cell's value (display text) without changing its formula?

查看:110
本文介绍了如何更改单元格的值(显示文本)而不更改其公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



因此,此宏会更改单元格 B2 (显示给我们的文本)的值,但要清除其公式。我需要更改 B2 的值,但也需要保留公式。



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



更新



我问这个问题是因为我试图解决这个问题

解决方案

我不知道这是否有帮助,因为这有点难告诉你的基本要求是什么,但是这里反过来:



几个事情影响单元格的显示


  1. 输入的值,如果它的一个常数

  2. 计算的结果,如果它的公式

  3. 单元格的格式

  4. 单元格的条件格式(如果有)

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



您可以使这个显示通过应用单元格格式的其他东西,

,例如格式为 0; 0; 0; Ov\e\r\ri\d\d\ e \\\\\\\\\\\\\\\\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

这可以通过应用VBA与

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

 范围(B2)。NumberFormat =0; 0 ; 0;覆盖文本


I've a problem with this VBA macro.

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

My test is here. To run this macro, open it in Excel, press Ctrl+F8 and choose 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.

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

解决方案

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 display of 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

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

You can make this display something else by applying a cell format,
eg a format of 0;0;0;Ov\e\r\ri\d\d\e\n t\ext will display any string value as Overridden text

This can by applied with VBA with

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"""

这篇关于如何更改单元格的值(显示文本)而不更改其公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆