【问题标题】:Cannot change date format with VBA in some Excel versions在某些 Excel 版本中无法使用 VBA 更改日期格式
【发布时间】:2019-02-10 15:48:15
【问题描述】:

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

我试过了

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

覆盖格式并将它们全部匹配到相同的格式,但在数据透视表中,上半部分始终显示 Mar/31/2018 而不是 03/31/2018。并且 2018 年 3 月 31 日日期左对齐,而另一半日期右对齐正确格式 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.

我希望能够将一般格式覆盖为适当的日期格式。

【问题讨论】:

  • 尝试使用CDate。它将字符串转换为日期。当从另一个应用程序或网站导出数据时,它通常以文本字符串的形式出现。另一种选择是复制粘贴乘以 1。
  • 会试一试。 CDate 会在已经正确的单元格上抛出错误吗?
  • cdate 不会在日期输入上引发错误。 debug.print format(cdate(dateserial(2018,6,3)),"YYYY-MM-DD") => 2018-06-03

标签: excel vba


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的某些“日期”可能实际上是 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
    

    【讨论】:

    • 是的,似乎它们只是一个具有一般单元格格式的字符串。我可以在所有单元格(甚至那些已经正确的单元格)上运行它,对吗?
    • @MJ95 是的......只需确保代码中的月份缩写与数据中的缩写匹配!
    • 有道理,会试试看。谢谢!
    • 您可能想在 nice konvert 函数中使用 LCase! ;-)
    • @simple-solution 好主意,可能想使用 Proper() 或其他替代方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多