我如何将字符串 "01May2020 "转换为日期值?

问题描述 投票:0回答:1

如何在VBA中把字符串 "01May2020 "转换为日期值?

DateValue()函数返回一个类型错误。

excel-vba
1个回答
0
投票

也许你可以使用像下面的函数。

Function GetMonth(ByVal dateString As String) As Date

    Dim dateStr As String, mStr As String, dVals() As String, mMumber As String

    dVals = Split(dateString, "/")
    If UBound(dVals) > 1 Then
        mStr = dVals(1)
        Select Case mStr
            Case "January"
                mMumber = "01"
            Case "February"
                mMumber = "02"
            Case "March"
                mMumber = "03"
            Case "April"
                mMumber = "04"
            Case "May"
                mMumber = "05"
            Case "June"
                mMumber = "06"
            Case "July"
                mMumber = "07"
            Case "August"
                mMumber = "08"
            Case "September"
                mMumber = "09"
            Case "October"
                mMumber = "10"
            Case "November"
                mMumber = "11"
            Case "December"
                mMumber = "12"
        End Select
        dateStr = dVals(0) & "/" & mMumber & "/" & dVals(2)
        If IsDate(dateStr) Then
            GetMonth = dateStr
        End If
    End If

End Function

Sub Test()

    Dim dStr As String
    Dim dDate As Date

    dStr = "01/May/2020"
    dDate = GetMonth(dStr)

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.