vba 通过vba在excel中的“dd/mm/yyyy”日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25144096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:46:06  来源:igfitidea点击:

"dd/mm/yyyy" date format in excel through vba

dateexcel-vbavbaexcel

提问by deejay

I am trying to write a date in "dd/mm/yyyy" format in excel sheet thru excel-vba. I achieved it using Cells(1, 1).Value = Format(StartDate, "dd/mm/yyyy"). and my cell value comes out to be 30/04/2014, which is good.....

我正在尝试通过 excel-vba 在 excel 表中以“dd/mm/yyyy”格式写入日期。我使用Cells(1, 1).Value = Format(StartDate, "dd/mm/yyyy"). 并且我的单元格值为30/04/2014,这很好.....

But there is some weird problem... Since I have to iterate the startDate for whole month by everytime adding it by 1, so the next value comes out to be 1/5/2014instead of 01/05/2014until 12th of each month 12/5/2014and from 13th, the month is again changing to two digits 13/05/2014....

但是有一些奇怪的问题......因为我必须通过每次将 startDate 添加 1 来迭代整个月,所以下一个值出现1/5/2014而不是01/05/2014直到每个月的 12 日12/5/2014,从 13 日开始,月份再次改变到两位数13/05/2014....

I want it all in two digits as I have to again search for these dates using Range.Find method, in which I am passing value with "dd/mm/yyyy" fromat.

我希望它全部为两位数,因为我必须再次使用 Range.Find 方法搜索这些日期,在该方法中,我使用“dd/mm/yyyy” fromat 传递值。

回答by deejay

I got it

我知道了

Cells(1, 1).Value = StartDate
Cells(1, 1).NumberFormat = "dd/mm/yyyy"

Cells(1, 1).Value = StartDate
Cells(1, 1).NumberFormat = "dd/mm/yyyy"

Basically, I need to set the cell format, instead of setting the date.

基本上,我需要设置单元格格式,而不是设置日期。

回答by Martin Hymanson

Your issue is with attempting to change your month by adding 1. 1 in date serials in Excel is equal to 1 day. Try changing your month by using the following:

您的问题是尝试通过添加 1. 1 在 Excel 中的日期序列来更改您的月份等于 1 天。尝试使用以下方法更改您的月份:

NewDate = Format(DateAdd("m",1,StartDate),"dd/mm/yyyy")