Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 2007 VBA CDate方法接受的格式_Excel_Vba_Excel 2007 - Fatal编程技术网

Excel 2007 VBA CDate方法接受的格式

Excel 2007 VBA CDate方法接受的格式,excel,vba,excel-2007,Excel,Vba,Excel 2007,我正在VBA中为Excel2007中的特定单元格开发一个更改事件。 我想将输入这些单元格的两种不同格式的日期(随时间)转换为一种格式(美式) 下面是我的代码,它检查输入的日期是否为两种所需格式之一。crmdate是ActiveCell的字符串值: If RegexServiceManager.test(crmdate) Then outputDate = Format(CDate(crmdate), "MM/dd/yyyy hh:mm") Applicat

我正在VBA中为Excel2007中的特定单元格开发一个更改事件。 我想将输入这些单元格的两种不同格式的日期(随时间)转换为一种格式(美式)

下面是我的代码,它检查输入的日期是否为两种所需格式之一。crmdate是ActiveCell的字符串值:

    If RegexServiceManager.test(crmdate) Then
        outputDate = Format(CDate(crmdate), "MM/dd/yyyy hh:mm")
        Application.EnableEvents = False
        ActiveCell.Value = outputDate
        ActiveCell.NumberFormat = "MM/dd/yyyy hh:mm"
        Application.EnableEvents = True
    ElseIf RegexSiebel.test(crmdate) Then
        outputDate = CDate(crmdate)
        Application.EnableEvents = False
        ActiveCell.Value = outputDate
        ActiveCell.NumberFormat = "MM/dd/yyyy hh:mm"
        Application.EnableEvents = True
    Else
        MsgBox "Inapropriate date and time format"
    End If
RegexServiceManager检查日期是否为YYYY/MM/DD HH:MM:SS格式,这样可以正常工作。 RegexSiebel检查日期是否为int DD-MMM-YYYY HH:MM格式,这就是问题开始的地方

我在
outputDate=CDate(crmdate)
行上得到一个“类型不匹配”错误。我已经删除了类似于上面“If”中的格式化方法,以确认错误来自CDate

有人能就此提出建议吗?CDate可能不识别DD-MMM-YYYY(示例:2013年1月1日)格式?如果是这样的话,谁能提出一个解决办法

目标格式为MM/DD/YYYY HH:MM

谢谢你,并致以最良好的问候

马西耶

编辑:


outputDate是日期格式

我想我找到了答案。这有点傻,但上面的代码不适用于波兰地区设置。它适用于美国(可能也适用于英国)地区环境

我还将outputDate更改为Variant类型

我的结局是:

    If RegexServiceManager.test(crmdate) Then
        MsgBox "otputDate: " & TypeName(outputDate) & vbCrLf & "crmdate: " & TypeName(crmdate)
        outputDate = CDate(crmdate)
        MsgBox "otputDate: " & TypeName(outputDate) & vbCrLf & "crmdate: " & TypeName(crmdate)
        Application.EnableEvents = False
        ActiveCell.Value = outputDate
        ActiveCell.NumberFormat = "MM/dd/yyyy hh:mm"
        Application.EnableEvents = True
    ElseIf RegexSiebel.test(crmdate) Then
        MsgBox "otputDate: " & TypeName(outputDate) & vbCrLf & "crmdate: " & TypeName(crmdate)
        outputDate = CDate(crmdate)
        MsgBox "otputDate: " & TypeName(outputDate) & vbCrLf & "crmdate: " & TypeName(crmdate)
        Application.EnableEvents = False
        ActiveCell.Value = outputDate
        ActiveCell.NumberFormat = "MM/dd/yyyy hh:mm"
        Application.EnableEvents = True
    Else
        MsgBox "Inapropriate date and time format"
    End If
消息框仅用于调试目的

在程序开始时检测区域设置可能是明智的,或者以更好的方式编写以避免这种情况。:)

希望这对别人有帮助


谢谢您并向您致意,

CDate()
确实识别dd-mmm-yyy-hh:mm格式。例如,尝试使用
cdate(格式(now(),“dd-mmm-yyy-hh:mm”)
。您好,creamyegg,谢谢您的提示。我想我找到了naswer。我将把它作为一个答案发布。+1,这是一个写得很好、很有见解的答案,并且确定了一个独特的问题。