Excel VBA: prevent Excel from formatting the string as a date

Quick question:

I want to insert a row like 10/3 into a cell like this:

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

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

How can I achieve this using VBA?

+4
source share
3 answers
 Worksheets("Sheet1").Range("A1").NumberFormat = "@" Worksheets("Sheet1").Range("A1").Value = "10/3" 
+11
source

add one quotation mark ' to the value - in your example it will make a string

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

or, if you have a variable containing data

 Worksheets("Sheet1").Range("A1").Value = "'" & MyValue 
+7
source

The solution .NumberFormat = "@" also worked for me. Thank you very much, unfortunately, beginners may not give msarks, but I would give 10 out of ten. My snippet:

 Cells(Row, Column).NumberFormat = "@" Cells(Row, Column).Value = Trim(CellValue) 
0
source

Source: https://habr.com/ru/post/1490746/


All Articles