简体   繁体   English

在Excel中将单元格值引用为字符串

[英]Reference cell value as string in Excel

In Excel, if the cell A1 has some value that gets formatted in a specific way, is there a way for cell B1 to reference the string displayed in A1? 在Excel中,如果单元格A1有一些以特定方式格式化的值,单元格B1是否有方法引用A1中显示的字符串?

To clarify: 澄清:

  • If A1 displays, for instance, the time 10:31:48, I wish to have B1 reference this outputted string as shown to the user ("10:31:48", not the underlying numerical representation "0.43875"). 例如,如果A1显示时间10:31:48,我希望B1将这个输出的字符串引用给用户(“10:31:48”,而不是基础数字表示“0.43875”)。
  • I'm well aware that there are functions for manually formatting values. 我很清楚,有手动格式化值的功能。 However, what I'm looking for is copying an already formatted value from another cell, no matter what format that cell may have. 但是,我正在寻找的是从另一个单元复制已经格式化的值,无论该单元格可能具有什么格式。

Is something like this possible? 这样的事情可能吗?

In fact, Excel stores datetime as a number , so you have to explicitly set format of the cell to see the proper value. 实际上,Excel将datetime存储为number ,因此您必须显式设置单元格的格式以查看正确的值。

You may want to use TEXT function, but anyway, you have to specify format of output string: 您可能想要使用TEXT函数,但无论如何,您必须指定输出字符串的format

=TEXT(A1,"hh:mm:ss")

Another option is to write your own VBA function, which can convert a value of a cell based on it's format: 另一种选择是编写自己的VBA函数,它可以根据格式转换单元格的值:

Public Function GetString(ByVal cell As Range) As String
 GetString = Format(cell, cell.NumberFormat)
End Function

This will give you a result based on source cell's format 这将根据源单元格的格式给出结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM