4

我需要在单元格中插入行,但我无法插入换行符。

例如 :

第1行

线2

第 3 行

使用 VBA 代码:

             Ws.Cells(i, 2) = line1 & line2 & line3

我得到:

第1行 第2行 第3行

我该如何解决这个问题?

4

5 回答 5

15

这是你正在尝试的吗?

Ws.Cells(i, 2).Value = "line1" & vbnewline & "line2" & vbnewline & "line3"

或者

Ws.Cells(i, 2).Value = "line1" & vbCrLf & "line2" & vbCrLf & "line3"

编辑:插入我的评论中提到的引号。

于 2012-05-27T15:48:33.463 回答
3

我已经测试了一些组合,结果如下:

cell(a,b) = line1 & vbCrLf & line2

结果:
第 1 行**
第 2 行

cell(a,b) = line1 & vbCr & line2

结果:
line1line2

cells(a,b) = line1 & vbLf & line2

结果:
第1行第
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. My preference goes for vbLf.

于 2012-11-29T08:04:43.073 回答
0

VBA 中的换行符是vbCrLf,您可以将它们与字符串连接起来。

于 2012-05-27T15:48:45.243 回答
0
Ws.Cells(i, 2) = line1 & line2 & Chr(10) & line3

按照这个答案。

于 2012-05-27T15:49:34.957 回答
-2

仅供参考,您可以通过使用更易于键入的变量名称来防止编码拼写错误。

bx = vbCrLf

textstring = "Hello everybody!" & bx & "This is my second line!"
于 2012-05-28T02:17:14.053 回答