Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

vba - Changing date formats in Excel using

I have a excel sheet in which a column has date date in the format "yyyyMMdd" and I want to format it as "yyyy/MM/dd".

For this I tried to use following line inside macro, but it's converting cell data as "###.....#" instead of changing date format.

Sheet1.Range("C3", "C302").NumberFormat = "yyyy/mm/dd"
...
result = "#####...#"
...

Can someone tell me why it's happening? Is there any other way for doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If a date/time cell appears full of # signs, it means that the column is too narrow to display the format.

Make the column wider to accommodate the full width of the selected date format.

See this screenshot. Both columns have the same format. Column A is too narrow to show the dates. Column B is wide enough.

enter image description here

Edit after discussing in chat:

The screen shot you posted in chat is this: enter image description here

The "dates" you are referring to are not dates. They are numbers that are way higher than what Excel uses for dates in this millenium.

Excel stores dates as whole numbers, starting as 1 for 1/1/1900. What you show in your screenshot are numbers way higher than Excel dates.

enter image description here

Your number 20150930 is NOT what Excel considers Sep-30-2015. For Excel, that date would be the number 42277, which you can perfectly format as that date.

The reason that your "dates" formatted with your format string come out as ##### is that the numbers are way higher than what Excel can interpret as dates.

You will need to convert your numbers to real Excel dates, which you can do with a simple formula. With your first "date" number in cell A1, you can use the formula

=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))

to return a value that Excel regards as a true date for Sep-30-2015 in this screenshot:

enter image description here

So, the reason for all the # signs is that the numbers you are trying to format as dates are too big for dates in Excel's algorithms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...