在宏 VBA 中将日期格式 MM-DD-YYYY 更改为 MM/DD/YYYY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44449139/
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-12 12:42:53  来源:igfitidea点击:

Change Date format MM-DD-YYYY to MM/DD/YYYY in macro VBA

excel-vbavbaexcel

提问by Sukanya

I tried using format function as below

我尝试使用格式功能如下

let the startdate = "06-05-2017"
End Date = format (startdate," MM/DD/YYYY"), but it is not getting changed. End Dateis still showing as 06-05-2017. Please help me know what is wrong here.

startdate = "06-05-2017"
End Date = format (startdate," MM/DD/YYYY"),但它没有改变。 End Date仍然显示为06-05-2017。请帮助我知道这里出了什么问题。

Thanks !!

谢谢 !!

回答by teylyn

Excel probably does not recognise the start date as a date, but as text. You need to work out what your system's default date format is. To do that, you can type this in the immediate window:

Excel 可能无法将开始日期识别为日期,而是将其识别为文本。您需要确定系统的默认日期格式是什么。为此,您可以在即时窗口中输入:

?date

The returned result shows in the date format you need to use to assign a value to your start date variable. On my computer I need to use dd/mm/yyyy, so for me, this code works:

返回的结果以您需要用来为开始日期变量赋值的日期格式显示。在我的电脑上我需要使用dd/mm/yyyy,所以对我来说,这段代码有效:

Sub test()
Dim startdate As Date
Dim enddate As String
startdate = "24/12/2017"
enddate = Format(startdate, "mm/dd/yyy")
Debug.Print enddate ' will show as 12/24/2017
End Sub