在某些Excel版本中无法使用VBA更改日期格式

时间:2019-02-10 15:48:16

标签: excel vba

在某些版本的Excel中,我有一半的日期为Mar / 31/2018格式,单元格格式为常规格式,另一半为日期格式和03/31/2018。这些是从某个地方导出的,所以我无法更改。这些日期用在数据透视表中。

我尝试过

Range("C2:C200").NumberFormat = "m/dd/yyyy"

覆盖格式并将它们全部匹配为相同格式,但是在数据透视表中,上半部分始终显示Mar / 31/2018而不是03/31/2018。并且Mar / 31/2018日期保持左对齐,而另一半日期则以正确的格式03/31/2018对齐。

Range("C2:C200").Value.NumberFormat = "m/dd/yyyy" does not work either. 

For i = 2 to lastRow Step 1
dateString = Cells(i, 3).Value
Cells(i, 3).Value = DateValue(dateString) only works for the cells that are already in custom or date format and not for the general format cells.

我希望能够将常规格式替换为正确的日期格式。

2 个答案:

答案 0 :(得分:2)

enter image description here

Option Explicit

Sub Convert2Date()
    Dim iCt As Integer
    Dim lastRow As Long
    Dim dateStr As Variant
    Dim dateArr() As String
    Dim YearInt As Integer, MonInt As Integer, dayInt As Integer
    'Range("C2:C200").Value.NumberFormat = "m/dd/yyyy" 'does not work either.
    'For i = 2 To lastRow
        'dateString = Cells(i, 3).Value
        'Cells(i, 3).Value = DateValue(dateString) 'only works for the cells that are already in custom or date format and not for the general format cells.


    lastRow = Range("C1").SpecialCells(xlCellTypeLastCell).Row
    For iCt = 2 To lastRow
        dateStr = Cells(iCt, 3).Value
        If Not (dateStr = "") Then
            If IsDate(dateStr) Then
                Cells(iCt, 5) = "isdate = OK!"
                Cells(iCt, 4) = CDate(dateStr)
            Else
                'Cells(iCt, 4) = CDate(dateStr)
                dateArr = Split(dateStr, "/")
                MonInt = ConvertMonth(dateArr(0))
                dayInt = CInt(dateArr(1))
                YearInt = CInt(dateArr(2))
                Cells(iCt, 4).Value = DateSerial(YearInt, MonInt, dayInt)
                Cells(iCt, 5) = "CONVERTED"
                Cells(iCt, 5).Interior.Color = vbYellow
            End If
        End If
    Next iCt
End Sub

Function ConvertMonth(MonthStr As String) As Integer
    Dim tempStr As String
    Dim tempInt As Integer
    tempStr = LCase(MonthStr)
    Select Case tempStr
        Case "jan"
            tempInt = 1
        Case "feb"
            tempInt = 2
        Case "mar"
            tempInt = 3
        Case "apr"
            tempInt = 4
        Case "may"
            tempInt = 5
        Case "jun"
            tempInt = 6
        Case "jul"
            tempInt = 7
        Case "aug"
            tempInt = 8
        Case "sep"
            tempInt = 9
        Case "oct"
            tempInt = 10
        Case "nov"
            tempInt = 11
        Case "dec"
            tempInt = 12
        Case Else
            Debug.Print "undefined month string"
            tempInt = 0
    End Select
End Function

答案 1 :(得分:1)

您的某些“日期”实际上可能是 Text 值。将它们转换为通用的“真实”格式。选择单元格并运行:

Sub DateUnifier()
    Dim r As Range, d As Date, s As String, nf As String, arry
    nf = "m/d/yyyy"
    For Each r In Selection
        s = r.Text
        If s <> "" Then
            arry = Split(s, "/")
            If UBound(arry) = 2 Then
                If IsNumeric(arry(0)) Then
                    r.Clear
                    r.Value = DateValue(s)
                    r.NumberFormat = nf
                Else
                    r.Clear
                    r.Value = DateSerial(CInt(arry(2)), konvert(arry(0)), CInt(arry(1)))
                    r.NumberFormat = nf
                End If
            End If
        End If
    Next r
End Sub

Public Function konvert(st As Variant) As Integer
    mnths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    i = 1
    For Each mn In mnths
        If st = a Then
            konvert = i
            Exit Function
        End If
    Next mn
End Function

更正:

konvert()函数存在错误,请改用此命令:

Public Function konvert(st As Variant) As Integer

    mnths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    i = 1
    For Each mn In mnths
        If st = mn Then
            konvert = i
            Exit Function
        End If
        i = i + 1
    Next mn
End Function