TylerDurden
TylerDurden

Reputation: 1670

New line character in excel when printing from vb.net

I am trying to print a value to a cell that contains a line seperator like ALT+ENTER or Char(10). Everything so far just prints my text so that it prints the values I want to wrap in the excel are in different cells

In my vb.net code I keep building a string as I loop through reports and I have tried vbcrlf, vblf etc to try and mimic a ALT+ENTER entry in excel using something similar to the below

_string = _string & vbcrlf & report.name &" " & _pctstatus &"%"

and then I paste this string into A1 using

Clipboard.SetDataObject(_string.ToString, False)
aRange.Select()
aWorkSheet.Paste()

In the picture you can see I keep getting the values in A3 but I really want the result in A1

Excel output

I can get around this by structuring my text as formula so that Char(10) is in the cell formula but I would prefer just enter a string value and have no formulae. I also have to do this as I am constrained by the number of free cells in the excel report since I am adding this an excel sheet heavily populated already

Upvotes: 2

Views: 3008

Answers (3)

iDevlop
iDevlop

Reputation: 25262

Just use vbLf instead of vbCrLf.

Upvotes: 0

Bradley Uffner
Bradley Uffner

Reputation: 16991

Is the format of the cell set to allow multiple lines (Wrap Text)?

Upvotes: 1

anefeletos
anefeletos

Reputation: 702

So is any special need to use clipboard? If not try this:

aRange.Value = _string.ToString

Upvotes: 1

Related Questions