【问题标题】:excel enter dates with or without slashesexcel输入带或不带斜线的日期
【发布时间】:2016-07-21 10:59:18
【问题描述】:

我有 VBA 代码将 6 位数字转换为带有斜杠的日期,即 311215 变为 31/12/2015,但我也希望用户也能够输入带有斜杠的日期。

使用下面的代码,31/12/15 变为 23/04/1969,01/01/15 变为 20/04/2005(?? - 明信片上的答案)。

Private Sub worksheet_change(ByVal target As Range)

Dim StrVal As String
Dim dDate As Date

If target.Cells.Count > 1 Then Exit Sub

If Intersect(target, Range("D7")) Is Nothing Then Exit Sub

With target

    StrVal = Format(.Text, "000000")

      If IsNumeric(StrVal) And Len(StrVal) = 6 Then

         Application.EnableEvents = False

        If Application.International(xlDateOrder) = 1 Then 'dd/mm/yy

            dDate = DateValue(Left(StrVal, 2) & "/" & Mid(StrVal, 3, 2) & "/" & Right(StrVal, 2))

        Else 'mm/dd/yy

            dDate = DateValue(Mid(StrVal, 3, 2) & "/" & Left(StrVal, 2) & "/" & Right(StrVal, 2))

        End If

        .NumberFormat = "dd/mm/yyyy"

        .Value = CDate(DateSerial(Year(dDate), Month(dDate), Day(dDate)))

      End If

End With

Application.EnableEvents = True

End Sub

我还需要包含验证,以便在单元格中只输入一个日期,因为这在许多其他子项中使用

【问题讨论】:

    标签: excel vba date


    【解决方案1】:

    您的字符串31/12/15 被评估为日期并由Format(.Text, "000000") 转换为内部整数表示42369(这是自1900 年以来的天数)。您的 Format 命令不会删除斜杠,而是将值解释为整数字符串。之后,您的代码将此数字转换为 23/04/1969

    你可以尝试更换你的

    StrVal = Format(.Text, "000000")

    StrVal = Replace(.Text, "/", "").

    【讨论】:

      【解决方案2】:

      此代码假定 D7 已被格式化为 Text before 任何用户条目:

      Private Sub worksheet_change(ByVal target As Range)
          Dim StrVal As String
          Dim dDate As Date
      
          If target.Cells.Count > 1 Then Exit Sub
          If Intersect(target, Range("D7")) Is Nothing Then Exit Sub
          Application.EnableEvents = False
          With target
              StrVal = .Text
              If IsNumeric(StrVal) And Len(StrVal) = 6 Then
                  If Application.International(xlDateOrder) = 1 Then 'dd/mm/yy
                      dDate = DateValue(Left(StrVal, 2) & "/" & Mid(StrVal, 3, 2) & "/" & Right(StrVal, 2))
                  Else 'mm/dd/yy
                      dDate = DateValue(Mid(StrVal, 3, 2) & "/" & Left(StrVal, 2) & "/" & Right(StrVal, 2))
                  End If
                  .NumberFormat = "dd/mm/yyyy"
                  .Value = dDate
              Else
                  ary = Split(StrVal, "/")
      
                  If Len(ary(2)) = 2 Then ary(2) = "20" & ary(2) 'fix the year if necessary
      
                  If Application.International(xlDateOrder) = 1 Then 'dd/mm/yy
                      dDate = DateValue(ary(2) & "/" & ary(1) & "/" & ary(0))
                  Else 'mm/dd/yy
                      dDate = DateValue(ary(2) & "/" & ary(0) & "/" & ary(1))
                  End If
                  .NumberFormat = "dd/mm/yyyy"
                  .Value = dDate
              End If
          End With
          Application.EnableEvents = True
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2013-06-25
        • 1970-01-01
        相关资源
        最近更新 更多