[英]Excel-VBA: Unable to Insert Line Breaks
I need to insert lines in a cell, but I am unable to insert line breaks.我需要在单元格中插入行,但我无法插入换行符。
For example :例如 :
line1
第一行
line2
线2
line3
线3
With VBA code :使用 VBA 代码:
Ws.Cells(i, 2) = line1 & line2 & line3
I get :我得到:
line1 line2 line3
线 1 线 2 线 3
How can I resolve this issue?我该如何解决这个问题?
Is this what you are trying?这是你正在尝试的吗?
Ws.Cells(i, 2).Value = "line1" & vbnewline & "line2" & vbnewline & "line3"
or要么
Ws.Cells(i, 2).Value = "line1" & vbCrLf & "line2" & vbCrLf & "line3"
EDIT : Inserted quotes as mentioned in my comments.编辑:插入了我的评论中提到的引号。
I have tested a few combinations and here are the results:我测试了几种组合,结果如下:
cell(a,b) = line1 & vbCrLf & line2
result:结果:
line1**第 1 行**
line2线2
cell(a,b) = line1 & vbCr & line2
result:结果:
line1line2线1线2
cells(a,b) = line1 & vbLf & line2
result:结果:
line1第一行
line2线2
In the above results the * denotes a blank space (I don't know why), so the vbCrLf is not recommended when you want to center the cell content horizontally.在上面的结果中,* 表示一个空格(我不知道为什么),所以当你想水平居中单元格内容时,不推荐使用 vbCrLf。 My preference goes for vbLf.
我更喜欢 vbLf。
VBA 中的换行符是vbCrLf
,您可以将它们与字符串连接起来。
Ws.Cells(i, 2) = line1 & line2 & Chr(10) & line3
FYI, you can prevent coding typos by using an easier-to-type variable name.仅供参考,您可以通过使用更易于键入的变量名称来防止编码拼写错误。
bx = vbCrLf
textstring = "Hello everybody!" & bx & "This is my second line!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.