将datetime字符串转换为有效日期值和有效时间值

时间:2016-02-09 21:35:50

标签: excel vba validation excel-vba datetime

此代码作为无效转换日期返回:

  

' 2014年9月10日星期三上午6:53'

VBA IsDate函数用于检查表达式是Date还是表达式可以转换为有效的DateTime。该函数返回truefalse

如何让日期有效?

'-----------------------------------------

Dim x As Integer

x = ActiveCell.Row

'-----------------------------------------

Dim vDate As Variant

vDate = "Wednesday, September 10, 2014 6:53 AM"

MsgBox vDate

MsgBox IsDate(vDate) 'retuns false

'-----------------------------------------

Dim fDAT As String

fDAT = Left(vDate, Len(vDate) - 7)

MsgBox fDAT

MsgBox IsDate(fDAT) 'retuns false

'-----------------------------------------

''ActiveSheet.Range("a" & x).Value = fDAT

3 个答案:

答案 0 :(得分:1)

你会改变:

fDAT = Left(vDate, Len(vDate) - 7)

要:

fDAT = Mid(vDate, InStr(vDate, ",") + 2)

答案 1 :(得分:0)

摆脱"星期三,"并尝试类似的事情:

@tempTable

答案 2 :(得分:0)

以获得正确性,你可以使用类似的东西:

https://github.com/kwhat/jnativehook/releases/

如果不是正确的工作日,这也将是假的;)

要获得可用的价值,您可以使用以下内容:

Sub blabla()
  Dim vDate As Variant, xDate As Boolean
  vDate = "Wednesday, September 10, 2014 6:53 AM"
  xDate = IsDate(vDate) Or (IsDate(Mid(vDate, InStr(vDate, ",") + 1)) And Left(vDate, InStr(vDate, ",") - 1) = Format(Mid(vDate, InStr(vDate, ",") + 1), "dddd"))
End Sub