Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the numberformat code for Short date?

Tags:

excel

vba

I know that I can write the following to format a number as a date with the appearance yyyy-mm-dd:

Range("A1").NumberFormat = "yyyy-mm-dd"

I know that I can also write:

Range("A1").NumberFormat = "m/d/yyyy"

and all kinds of things.

But is there a way I can set the number format to become a general short date and leave it up to the user's system to decide the exact appearance?

I'm looking for an expression like:

Range("A1").NumberFormat = "Short Date" '(<-This does not work)
like image 202
user1283776 Avatar asked Jan 19 '15 08:01

user1283776


People also ask

How do you format a short date number?

When you type something like 2/2 in a cell, Excel for the web thinks you're typing a date and shows it as 2-Feb. But you can change the date to be shorter or longer. To see a short date like 2/2/2013, select the cell, and then click Home > Number Format > Short Date.

How do I format a short date in VBA?

To format a date in VBA, we use the inbuilt FORMAT function itself. It takes input as the date format and returns the desired format required. The arguments required for this function are the expression and the format type. Formatting date and time are sensitive things in Excel, and the same applies to VBA.


1 Answers

It is a bit tricky. If you have a look at a cell's properties, format tab, category Date, you will see some formats beginning with an asterisk (*), those respond to the changes of the user locale. Try setting your cell to the first element, short date, then get its numberformat value in the immediate window with ?activecell.NumberFormat. You'll see that it is "m/d/yyyy", so your snippet does the job well. Just try changing your locale setting in control panel, the format of the cell will change accordingly.

like image 71
ignotus Avatar answered Oct 01 '22 23:10

ignotus