Is it possible to insert line break in a wrapped cell through VBA code? (similar to doing Alt-Enter when entering data manually)
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.
It's easy to add a line break when you're typing in an Excel worksheet. Just click where you want the line break, and press Alt + Enter.
Example #2 – Insert New Line Using “Char (10)” To a new line instead of “vbNewLine,” we can also use the function CHR to insert a new line in VBA. read more. For example, CHR (10) is the code to insert a new line in VBA.
If you want to force a new line in a message box, you can include one of the following: The Visual Basic for Applications constant for a carriage return and line feed, vbCrLf. The character codes for a carriage return and line feed, Chr(13) & Chr(10).
Yes. The VBA equivalent of AltEnter is to use a linebreak character:
ActiveCell.Value = "I am a " & Chr(10) & "test"
Note that this automatically sets WrapText
to 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
You could also use vbCrLf
which corresponds to Chr(13)
& Chr(10)
. As Andy mentions in the comment below, you might be better off using ControlChars.Lf
instead though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With