Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newline in Excel Cell 3

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
I am generating an excel worksheet via csharp. Some of the header row cells are lengthy and I want to insert a newline character into cell, but I can't figure out what the ascii/char code is. I know it's not the following:
\r\n (white space)
Enviornment.NewLine (white space)
char(10) (white space)
char(13) (0 length character/non-character)

I know when I add a new line to Excel manually I need to hold the <alt> key. Would I need to account for the <alt> key to enter the new line programatically? If so what is the ascii code for <alt><enter>?

thank you for your assistance.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
It is char(10), or in VBA chr(10). But you have to format for alignment wrapping.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
In VBA:
ActiveCell.FormulaR1C1 = "AB" & Chr(10) & "CD"
or
ActiveCell.FormulaR1C1 = "AB" & vbCr & "CD"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You can use Chr(10)

eg. in VBA

Range("A1").value = "This is" & chr(10) & "a test"

will result in a 2 line cell display

The standard is to use vbcrlf (carriage return + line feed) but that adds a non printable character to the cell as well as the new line - as does vbNewLine

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Oops, sorry for the typo:
In VBA:
ActiveCell.FormulaR1C1 = "AB" & Chr(10) & "CD"
or
ActiveCell.FormulaR1C1 = "AB" & vb[!]Lf[/!] & "CD
 
Word wrap fixed the problem. using csharp the constant is Enviornment.NewLine

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Glenn - funnily enough, that was what I thought but if you run my (v.small) code snippet on a cell with alignment not set to wrap, not only does it work but it automatically sets the wrap property to TRUE - might be a 2003 thing as we have just been upgraded - does it work like this for you ??

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Geoff,

Another star to you for the tip on using Chr(10) vs. vbCrLf or vbNewLine. A simple thing but very helpful, once known!


Regards,
Mike
 
Hi Geoff,

it works like that for me too ( Excel 97 ). But I know I've had problems like that in the past, and it was alignment. I should have done a little test like you.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
cheers for the update Glenn - always good to know what happens in other versions as well !!

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top