vba 通过代码在包裹的单元格中插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9900916/
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
Insert line break in wrapped cell via code
提问by Sarika.S
Is it possible to insert line break in a wrapped cell through VBA code? (similar to doing Alt-Enterwhen entering data manually)
是否可以通过 VBA 代码在包装的单元格中插入换行符?(类似于做Alt-Enter手动输入数据时)
I have set the cell's wrap text property to True via VBA code, and I am inserting data into it also through VBA code.
我已通过 VBA 代码将单元格的换行文本属性设置为 True,并且我也通过 VBA 代码将数据插入其中。
回答by brettdj
Yes. The VBA equivalent of AltEnteris to use a linebreak character:
是的。VBA 等效AltEnter于使用换行符:
ActiveCell.Value = "I am a " & Chr(10) & "test"
Note that this automatically sets WrapText
to True.
请注意,这会自动设置WrapText
为 True。
Proof:
证明:
Sub test()
Dim c As Range
Set c = ActiveCell
c.WrapText = False
MsgBox "Activcell WrapText is " & c.WrapText
c.Value = "I am a " & Chr(10) & "test"
MsgBox "Activcell WrapText is " & c.WrapText
End Sub
回答by Andy Brown
You could also use vbCrLf
which corresponds to Chr(13)
& Chr(10)
.
您也可以使用vbCrLf
which 对应于Chr(13)
& Chr(10)
。
回答by Origamer7
Yes there are two way to add a line feed:
是的,有两种添加换行符的方法:
Use the existing function from VBA
vbCrLf
in the string you want to add a line feed, as such:Dim text As String
text = "Hello" & vbCrLf & "World!"
Worksheets(1).Cells(1, 1) = text
Use the
Chr()
function and pass the ASCII characters 13 and 10 in order to add a line feed, as shown bellow:Dim text As String
text = "Hello" & Chr(13) & Chr(10) & "World!"
Worksheets(1).Cells(1, 1) = text
vbCrLf
在要添加换行符的字符串中使用 VBA中的现有函数,如下所示:将文本变暗为字符串
text = "Hello" & vbCrLf & "World!"
Worksheets(1).Cells(1, 1) = 文本
使用该
Chr()
函数并传递 ASCII 字符 13 和 10 以添加换行符,如下所示:将文本变暗为字符串
text = "Hello" & Chr(13) & Chr(10) & "World!"
Worksheets(1).Cells(1, 1) = 文本
In both cases, you will have the same output in cell (1,1) or A1.
在这两种情况下,您将在单元格 (1,1) 或 A1 中获得相同的输出。
回答by Linus Angel
Just do Ctrl+ Enterinside the text box
只需在文本框内执行Ctrl+Enter