Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

excel - VBA code to set date format for a specific column as "yyyy-mm-dd"

I have a macro which I specify the date (in mm/dd/yyyy) in a textbox and I want to set this value for column A in yyyy-mm-dd format. I have the following code:

Sheets("Sheet1").Range("A2", "A50000").Value = TextBox3.Value
Sheet1.Range("A2", "A50000") = Format(Date, "yyyy-mm-dd")

...and when I run the macro, the date is still in mm/dd/yyyy format.

How can I change this so that it is in the format I want?? I've been trying many kinds of code researched through google and nothing will set the format the way I want it.

Any help will be appreciated...

EDIT: Full code from OP's comment below:

 Workbooks.Add
 Range("A1") = "Acctdate"
 Range("B1") = "Ledger"
 Range("C1") = "CY"
 Range("D1") = "BusinessUnit"
 Range("E1") = "OperatingUnit"
 Range("F1") = "LOB"
 Range("G1") = "Account"
 Range("H1") = "TreatyCode"
 Range("I1") = "Amount"
 Range("J1") = "TransactionCurrency"
 Range("K1") = "USDEquivalentAmount"
 Range("L1") = "KeyCol"
 Sheets("Sheet1").Range("A2", "A50000").Value = TextBox3.Value
 Sheet1.Range("A2", "A50000").NumberFormat = "yyyy-mm-dd"
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use the range's NumberFormat property to force the format of the range like this:

Sheet1.Range("A2", "A50000").NumberFormat = "yyyy-mm-dd"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...