vba 如何在 Excel 中获取显示值而不是实际值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099685/
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-11 10:33:49  来源:igfitidea点击:

How can I obtain the displayed value instead of actual value in Excel?

excelvbadatecopyformat

提问by JC.

I have a cell containing a date ex. "05/11/09" It is currently displayed as "11-MAY-09". How do I copy-paste or use VBA to get the string "11-MAY-09" into the cell next to it ( NOT "05/11/09")?

我有一个包含日期前的单元格。“05/11/09” 当前显示为“11-MAY-09”。如何复制粘贴或使用 VBA 将字符串“11-MAY-09”放入它旁边的单元格中(不是“05/11/09”)?

I can't figure it out other than piecing out the date pieces by itself.

除了自己拼出日期片之外,我无法弄清楚。

采纳答案by mandroid

Use the Format function.

使用格式功能。

Format("5/11/2009", "DD-MMM-YY")

This will return:

这将返回:

11-May-09

If case matters:

如果案件重要:

UCase(Format("5/11/2009", "DD-MMM-YY"))

returns:

返回:

11-MAY-09

回答by Chuck Dickens

Range("B1").Value = Range("A1").Text

Using the cell's .text instead of .value modifier will copy the text formatting instead of the raw date.

使用单元格的 .text 而不是 .value 修饰符将复制文本格式而不是原始日期。

Chuck

查克

回答by dpmattingly

I believe you can use the TEXT function to format a date value to your liking.

我相信您可以使用 TEXT 函数根据自己的喜好设置日期值的格式。

The format string of "dd-mmm-yy" would format "05/11/09" as "11-MAY-09".

“dd-mmm-yy”的格式字符串会将“05/11/09”格式化为“11-MAY-09”。

回答by anonym

"The same answer but with a different function (that has worked for me):

“相同的答案,但具有不同的功能(这对我有用):

Public Function DisplayText(ByVal pRange As Range) As String  
  DisplayText = pRange.Text  
End Function  

Just use =DisplayText(A1). If you change the cell format this function will return the displayed text"

只需使用=DisplayText(A1). 如果您更改单元格格式,此函数将返回显示的文本”

  • cc: alvaroc
  • 抄送:阿尔瓦洛克

How can I get the displayed value of a cell in MS Excel ( for text that was converted to dates)?

如何获取 MS Excel 中单元格的显示值(对于转换为日期的文本)?

回答by Ryan Shannon

Try this:

尝试这个:

Sub FormattedText()
    Dim r As Range

    On Error Resume Next
    Set r = Application.InputBox(prompt:="Select cell", Type:=8)
    If r.Count <> 1 Or r Is Nothing Then
        Exit Sub
    End If
    On Error GoTo 0

    ActiveCell = "'" & r.Text

End Sub

It will put text of a selected cell (prompted) in the active cell.

它将在活动单元格中放置选定单元格(提示)的文本。

回答by Ryan Shannon

You should be able to right click on the cell and set the format as General. This will allow you to put something in without it being automatically formatted to something else.

您应该能够右键单击单元格并将格式设置为常规。这将允许您放入某些内容而不会自动将其格式化为其他内容。

To save yourself from copying and pasting you will also want to start by putting in the date you want and not formatting and then copying.

为了避免复制和粘贴,您还需要先输入您想要的日期而不是格式化然后复制。

回答by Bryan

In VBA you can do this:

在 VBA 中,您可以这样做:

Range("B2") = Range("A2")
Range("B2").NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005

If you need to loop it then:

如果您需要循环它,则:

Range("B" & i) = Range("A"& i)
Range("B" & i).NumberFormat = "dd-mmm-yyyy hh:mm:ss" 'Date as 10-Jun-2005

Another way to do it.

另一种方法来做到这一点。

回答by MVO

Low-tech but very easy way - paste it into Word, then copy back into Excel! Can take a while with a big file, however... buts works great for one-off uses!

技术含量低但非常简单的方法 - 将其粘贴到 Word 中,然后再复制回 Excel!处理大文件可能需要一段时间,但是……但是非常适合一次性使用!