Excel Hack 37: Working with carriage return in Excel VBA

So, you might be working on a code that returns lines or paragraphs of text into a cell or text box in Microsoft Excel, and you want to make sure that this text is presently in an orderly, parapraph-like fashion. This is where the carriage return + line feed code comes in handy. This code will space out the text by creating line breaks wherever it is inserted, so that a new paragraph is automatically formed. It has a similar effect to using Alt+Enter in regular Microsoft Excel when entering data into sheet cells. This code is vbCrLf (or Chr10).

This is basically how it works:

Code 1:

Cells(1,3).value = “Hi ” & “My name is Excel User”

Result:

PicturenoCarr

Code 2:

Cells(1,3).value = “Hi ” & Chr(10) & “My name is Excel User”

OR

Cells(1,3).value = “Hi ” & vbCrLf  & “My name is Excel User”

Result:

PictureCarr

 

Leave a Reply

Your email address will not be published. Required fields are marked *