【问题标题】:Formatting Text Dates to Excel Usable Date Format将文本日期格式化为 Excel 可用的日期格式
【发布时间】:2016-03-18 05:20:03
【问题描述】:

我有一个 Excel 文件,其中包含一些格式为“2016 年 3 月 6 日”的日期,我想将其转换为“d/MM/yyyy”或“6/3/2016”,以便使用像 @987654327 这样的 Excel 公式@ 以提取部分日期。

我写了一个小宏来帮助我解决这个问题,它只是像在输入数据集中手动替换日期一样。

Sub MonthReplace()
    Dim res As Boolean
    Dim i As Long
    Dim monthArray As Variant

    monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October",     "November", "December")    

    For i = LBound(monthArray) To UBound(monthArray)
        res = Range("RawDataset").Replace(" " + monthArray(i) + ", ", "/" + Format(i + 1) + "/")
    Next i

End Sub

结果是一个不稳定的数据集。请查看之前和之后的图像。

有些转换正确,而另一些则互换月份和日期。当我以前不使用宏替换月份时,不会出现此行为。 Excel 中的默认日期格式是根据我想要的格式设置的。

日期的系统区域设置:

【问题讨论】:

  • 您系统的默认短日期格式似乎是m/d/yyyy,而不是d/m/yyyy。见 3 月 6 日。 2016 => 2016 年 3 月 6 日和 3 月 2 日。 2016 => 2016 年 3 月 2 日。所以 3 月 17 日。 2016 年无法从 17/3/2016 转换为日期格式,因为第 17 个月未知。
  • @AxelRichter 添加了我的日期设置的屏幕截图。虽然你说的有道理,但是为什么 excel 的行为在手动和基于宏的方法中有所不同?
  • 有什么理由不用CDate来转换每个字符串?
  • 哦,对不起,我的错。我没有考虑过 VBA 的行为。 VBA 将始终为 en_usm/d/yy。所以必须要么设置这种格式,要么使用CDate("dd/mm/yyyy")进行转换。

标签: vba excel


【解决方案1】:

也许直接CDate 方法对您有用,因为您的系统和 Excel 语言似乎是英语。对我来说,它不起作用,因为我的系统不知道英文月份名称。所以我必须真正用数字替换它们:

Sub MonthReplace()

    Dim i As Long
    Dim monthArray As Variant
    Dim c As Range
    Dim strDate As String

    monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

    For Each c In Range("RawDataset")
     strDate = Replace(c.Value, " ", "")
     strDate = Replace(strDate, ",", "")
     For i = LBound(monthArray) To UBound(monthArray)
      strDate = Replace(strDate, monthArray(i), "/" & (i + 1) & "/", , , vbTextCompare)
      If IsDate(strDate) Then Exit For
     Next i
     If IsDate(strDate) Then c.Value = CDate(strDate)
    Next

End Sub

【讨论】:

    【解决方案2】:

    转换每个单元格可能更可靠:

    For Each Row In Range("RawDataset").Rows
      Row.Cells(1, 2) = CDate(Row.Cells(1, 1))
    Next
    

    【讨论】:

      【解决方案3】:

      在尝试了各种方法后,最终确定了这个方法,它将06 March, 2016 转换为06-March-2016,通过明确说明月份使其在 Excel 中可用(我的主要目标)以避免 VBA 日期格式问题。

      Sub MonthReplace()
          Dim res As Boolean
          Dim i As Long
          Dim endRow As Long
          Dim columnArray As Variant
      
          ' only work on columns containing the dates
          columnArray = Array("W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AL", "AM")
      
          ' find the last cell with data in the current sheet
          endRow = ActiveCell.SpecialCells(xlLastCell).Row
      
          For i = LBound(columnArray) To UBound(columnArray)
              With Range(columnArray(i) & "3:" & columnArray(i) & endRow)
                  res = .Replace(", ", "-")
                  res = .Replace(" ", "-")
              End With
          Next i
      End Sub
      

      此外,基于 +Axel Richter 的答案,通过检查 Cell.Value 中的错误并确保最后 4 个字符是数字,我写了以下内容。但是,这种方法非常慢,因为要检查每个单元格。可以使用上述策略(范围内的选定列)来提高速度。

      Sub MonthReplace_slow()
      
          Dim i As Long
          Dim monthArray As Variant
          Dim c As Range
          Dim strDate As String
      
          monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
      
          For Each c In Range("RawDataset")
              strDate = ""
              If Not IsError(c.Value) Then
              strDate = c.Value
                  If IsNumeric(Right(strDate, 4)) Then
                      strDate = Replace(strDate, " ", "")
                      strDate = Replace(strDate, ",", "")
                      For i = LBound(monthArray) To UBound(monthArray)
                          strDate = Replace(strDate, monthArray(i), "/" & (i + 1) & "/", , , vbTextCompare)
                          If IsDate(strDate) Then Exit For
                      Next i
                      If IsDate(strDate) Then c.Value = CDate(strDate)
                  End If
              End If
          Next
      
      End Sub
      

      我没有玩过其他 CDate 方法。

      【讨论】:

        猜你喜欢
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多