I need to insert lines in a cell, but I am unable to insert line breaks.
For example :
line1
line2
line3
With VBA code :
Ws.Cells(i, 2) = line1 & line2 & line3
I get :
line1 line2 line3
How can I resolve this issue?
I need to insert lines in a cell, but I am unable to insert line breaks.
For example :
line1
line2
line3
With VBA code :
Ws.Cells(i, 2) = line1 & line2 & line3
I get :
line1 line2 line3
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**
line2
cell(a,b) = line1 & vbCr & line2
result:
line1line2
cells(a,b) = line1 & vbLf & line2
result:
line1
line2
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.
Linebreaks in VBA are vbCrLf
and you can concatenate them with the strings.
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!"