Data does not transfer to excel correctly

sweit

Member
Joined
Mar 30, 2016
Messages
9
Programming Experience
10+
Hello,

I have a windows form with a text box to be filled in by the user with a number. The number is then written to the cell of an excel spread sheet. The cell on the spread sheet is formatted to display percent. The format is not transferring correctly. If the users types 1.75 into the textbox on the windows form, it shows up in the excel sheet as 175.00% How can I format this correctly.

There is a label on the windows form that is written to another cell on the worksheet also as a percentage and it works fine.

The code:

VB.NET:
xlWorkSheet2.Cells(24, 6) = txtRoom.Text
xlWorkSheet2.Cells(25, 6) = lblPercent3.Text

Thank you,
Steve
 
Numerically 1,75 is 175%, think about it - percent means one part in a hundred - 1 percent is 1/100 = 0,01. In a spreadsheet when you format a cell as percentage it is the application user interface that helps you when you type 1,75 it shows value 1,75%, but if you then change cell formatting to regular number you will see that the actual numeric value in that cell is 0,0175. Currently your code is assigning a string (that looks like a number) to a cell, I think you have to convert the string to a number, do the math, then assign the actual percentage number to the cell.
 
Numerically 1,75 is 175%, think about it - percent means one part in a hundred - 1 percent is 1/100 = 0,01. In a spreadsheet when you format a cell as percentage it is the application user interface that helps you when you type 1,75 it shows value 1,75%, but if you then change cell formatting to regular number you will see that the actual numeric value in that cell is 0,0175. Currently your code is assigning a string (that looks like a number) to a cell, I think you have to convert the string to a number, do the math, then assign the actual percentage number to the cell.


What you are saying makes sense but why does a value of 20.00 for lblPercent2.Text get stored in a cell, also formatted to display percent, as 20.00% instead of 2000.00%? The label text is transferring the way I would like but the textbox text is not. Any idea why?

Thank you,
Steve.
 
No, that is a mystery. Any chance that string already has a % character in it?
 
Back
Top