ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Force a cell to display as Text (https://www.excelbanter.com/excel-programming/371276-force-cell-display-text.html)

Julie

Force a cell to display as Text
 
I am programmatically (via C#) placing data on an Excel spreadsheet. Problem
is that some of the data look like dates (e.g "4/22") which really are not
dates. Excel is automatically converting these to dates. When using Excel, I
can set the cell's format category to "Text" and it will display properly.
How do I do that programatically?

witek

Force a cell to display as Text
 
Julie wrote:
I am programmatically (via C#) placing data on an Excel spreadsheet. Problem
is that some of the data look like dates (e.g "4/22") which really are not
dates. Excel is automatically converting these to dates. When using Excel, I
can set the cell's format category to "Text" and it will display properly.
How do I do that programatically?


add "'" at the beginning of your text.

var = "'" + var

and now Excel will see it as text.



Julie

Force a cell to display as Text
 

Thanks for your help... but I need a way without altering the data values
(via some kind of number formatting). For example, I tried

myRange.NumberFormat = ""; or
myRange.NumberFormat = "Text";

which didn't work. But I'm looking for a solution more along those lines.
Thanks a bunch!

"witek" wrote:

Julie wrote:
I am programmatically (via C#) placing data on an Excel spreadsheet. Problem
is that some of the data look like dates (e.g "4/22") which really are not
dates. Excel is automatically converting these to dates. When using Excel, I
can set the cell's format category to "Text" and it will display properly.
How do I do that programatically?


add "'" at the beginning of your text.

var = "'" + var

and now Excel will see it as text.




Die_Another_Day

Force a cell to display as Text
 
myRange.NumberFormat = "@"

Charles Chickering

Julie wrote:
Thanks for your help... but I need a way without altering the data values
(via some kind of number formatting). For example, I tried

myRange.NumberFormat = ""; or
myRange.NumberFormat = "Text";

which didn't work. But I'm looking for a solution more along those lines.
Thanks a bunch!

"witek" wrote:

Julie wrote:
I am programmatically (via C#) placing data on an Excel spreadsheet. Problem
is that some of the data look like dates (e.g "4/22") which really are not
dates. Excel is automatically converting these to dates. When using Excel, I
can set the cell's format category to "Text" and it will display properly.
How do I do that programatically?


add "'" at the beginning of your text.

var = "'" + var

and now Excel will see it as text.





Dave Peterson

Force a cell to display as Text
 
myRange.numberformat = "@"

Sometimes you can get the answer by recording a macro within excel.

Julie wrote:

Thanks for your help... but I need a way without altering the data values
(via some kind of number formatting). For example, I tried

myRange.NumberFormat = ""; or
myRange.NumberFormat = "Text";

which didn't work. But I'm looking for a solution more along those lines.
Thanks a bunch!

"witek" wrote:

Julie wrote:
I am programmatically (via C#) placing data on an Excel spreadsheet. Problem
is that some of the data look like dates (e.g "4/22") which really are not
dates. Excel is automatically converting these to dates. When using Excel, I
can set the cell's format category to "Text" and it will display properly.
How do I do that programatically?


add "'" at the beginning of your text.

var = "'" + var

and now Excel will see it as text.




--

Dave Peterson

Julie

Force a cell to display as Text
 
Worked perfectly. Thanks much!!! :-)

"Die_Another_Day" wrote:

myRange.NumberFormat = "@"

Charles Chickering



Julie

Force a cell to display as Text
 
The "@" symbol worked. Thanks.

I don't understand what you mean by "recording a macro within excel". I
really haven't done any macros in Excel, nor any VBA. I've only programmed in
..NET C#, using Visual Studio Tools For Office.

"Dave Peterson" wrote:

myRange.numberformat = "@"

Sometimes you can get the answer by recording a macro within excel.



witek

Force a cell to display as Text
 
1. Open excel, go to tools/Macro/Record new Macro.

2. Do, what you want to do with Excel, i.e. change cell formating to text.
3. Go to tools/Macro/Stop recording
4. Click Alt-F11 to open VB Editor
5. Find your project and usualy Module1 module.
6. Look at the code generated by macro recorder.

you will find:

Sub Macro1()
Selection.NumberFormat = "@"
End Sub


Now, you know how people know what to put as number format. :)




Julie wrote:
The "@" symbol worked. Thanks.

I don't understand what you mean by "recording a macro within excel". I
really haven't done any macros in Excel, nor any VBA. I've only programmed in
.NET C#, using Visual Studio Tools For Office.

"Dave Peterson" wrote:


myRange.numberformat = "@"

Sometimes you can get the answer by recording a macro within excel.





All times are GMT +1. The time now is 03:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com