Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Excel to format string as date

Tags:

excel

vba

I want to insert a string of type 10/3 in a cell like this:

Worksheets("Sheet1").Range("A1").Value = "10/3"

But Excel automatically formats the cell as a date or a number. I want to have it as a string.

How can I achieve this with VBA?

like image 527
karamell Avatar asked Jul 10 '13 15:07

karamell


2 Answers

Worksheets("Sheet1").Range("A1").NumberFormat = "@"
Worksheets("Sheet1").Range("A1").Value = "10/3"
like image 124
funk Avatar answered Oct 10 '22 18:10

funk


add a single quote ' before the value - in your example, it would make the line

Worksheets("Sheet1").Range("A1").Value = "'10/3"

or, if you have a variable that holds the data

Worksheets("Sheet1").Range("A1").Value = "'" & MyValue
like image 30
SeanC Avatar answered Oct 10 '22 17:10

SeanC