VBA - 转换为日期

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

VBA - convert to date

stringexcelvbaexcel-vbadate-conversion

提问by InformatikBabo

I have one more time a problem:

我还有一个问题:

I want to convert from Strings to dates in VBA

我想在 VBA 中从字符串转换为日期

The Strings look like: YYYY-DD-MM

字符串看起来像:YYYY-DD-MM

The date should be like: DD.MM.YYYY

日期应该是:DD.MM.YYYY

I know, normally you do this with the method cdate(), but it doesn't work here. I think it's because the structure of the string is bad to convert.

我知道,通常您使用 cdate() 方法来执行此操作,但它在这里不起作用。我认为这是因为字符串的结构不好转换。

thanks for your help

感谢您的帮助

InformatikBabo

InformatikBabo

回答by

Sub Main()

    Dim strDate As String
    strDate = "2013-06-11"

    Debug.Print "Original Date: ", strDate
    Debug.Print "CDate() Conversion: ", CDate(strDate)
    Debug.Print "Format() as String: ", Format(strDate, "DD.MM.YYYY")

End Sub

and the Immediate Window shows

和立即窗口显示

enter image description here

在此处输入图片说明