使用VBA将文本添加到Excel中的单元格中

时间:2022-03-21 05:48:02

I've been working with SQL and Excel Macros, but I don't know how to add text to a cell.

我一直在使用SQL和Excel宏,但我不知道如何向计算单元中添加文本。

I wish to add the text "01/01/13 00:00" to cell A1. I can't just write it in the cell because the macro clears the contents of the sheet first and adds the information afterwards.

我想在A1单元格中加入“01/01/13 00:00”。我不能把它写在单元格中,因为宏首先清除表的内容,然后添加信息。

How do I do that in VBA?

在VBA中如何做到这一点?

3 个解决方案

#1


15  

Range("$A$1").Value = "'01/01/13 00:00" will do it.

范围(“$ 1美元”)。Value = " 01/01/13 00:00"就可以了。

Note the single quote; this will defeat automatic conversion to a number type. But is that what you really want? An alternative would be to format the cell to take a date-time value. Then drop the single quote from the string.

注意单引号;这将击败自动转换为数字类型。但这是你真正想要的吗?另一种选择是将单元格格式化为获取日期-时间值。然后从字符串中删除单引号。

#2


9  

You could do

你可以做

[A1].Value = "'O1/01/13 00:00"

if you really mean to add it as text (note the apostrophe as the first character).

如果您真的想将它添加为文本(请注意撇号作为第一个字符)。

The [A1].Value is VBA shorthand for Range("A1").Value.

(A1)。Value是范围(“A1”)的VBA简写。Value。

If you want to enter a date, you could instead do (edited order with thanks to @SiddharthRout):

如果你想输入日期,你可以这样做(编辑顺序,感谢@SiddharthRout):

[A1].NumberFormat = "mm/dd/yyyy hh:mm;@"
[A1].Value = DateValue("01/01/2013 00:00")

#3


2  

You need to use Range and Value functions.
Range would be the cell where you want the text you want
Value would be the text that you want in that Cell

您需要使用范围和值函数。Range将是你想要的单元格你想要的文本值将是你想要的文本

Range("A1").Value="whatever text"

#1


15  

Range("$A$1").Value = "'01/01/13 00:00" will do it.

范围(“$ 1美元”)。Value = " 01/01/13 00:00"就可以了。

Note the single quote; this will defeat automatic conversion to a number type. But is that what you really want? An alternative would be to format the cell to take a date-time value. Then drop the single quote from the string.

注意单引号;这将击败自动转换为数字类型。但这是你真正想要的吗?另一种选择是将单元格格式化为获取日期-时间值。然后从字符串中删除单引号。

#2


9  

You could do

你可以做

[A1].Value = "'O1/01/13 00:00"

if you really mean to add it as text (note the apostrophe as the first character).

如果您真的想将它添加为文本(请注意撇号作为第一个字符)。

The [A1].Value is VBA shorthand for Range("A1").Value.

(A1)。Value是范围(“A1”)的VBA简写。Value。

If you want to enter a date, you could instead do (edited order with thanks to @SiddharthRout):

如果你想输入日期,你可以这样做(编辑顺序,感谢@SiddharthRout):

[A1].NumberFormat = "mm/dd/yyyy hh:mm;@"
[A1].Value = DateValue("01/01/2013 00:00")

#3


2  

You need to use Range and Value functions.
Range would be the cell where you want the text you want
Value would be the text that you want in that Cell

您需要使用范围和值函数。Range将是你想要的单元格你想要的文本值将是你想要的文本

Range("A1").Value="whatever text"