填写日期(jj/mm/yyyy) 只需输入天(jj)

Fill date (jj/mm/yyyy) only by entering the day (jj)

我有 12 个 12 个月的工作表,每个月的日期是随机的,要在 A 列中手动输入。我们以一月份为例:

当我在单元格 A1 中输入数字 25 时,我希望单元格自动 return 25/01/2019 in A1 (!)(或 01/25/2019,如您所愿)。 Excel 据我所知,即使使用自定义设置,自动填充功能也无法做到这一点,所以我猜:VBA?

我认为代码应该是这样的(带有更改事件):

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, cell As Range
    Set rng = Range("A:A")
    If Not Application.Intersect(rng, Range(Target.Address)) _
           Is Nothing Then

           '???
           'If cell entered in range is a number Then
           'change value to "number" & "/01/2019" 

    End If
End Sub

这就是我所在的位置。我很确定对于使用月份和输入多个日期的人来说,这可能是一段有用的代码。我离真相还远吗?它甚至可行吗?我知道这可能比听起来更复杂。

Option Explicit    

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedRange As Range
    Set AffectedRange = Application.Intersect(Me.Range("A:A"), Target)

    If Not AffectedRange Is Nothing Then
        Dim Cell As Range
        For Each Cell In AffectedRange.Cells
            If Cell.Value >= 1 And Cell.Value <= 31 Then
                Application.EnableEvents = False
                Cell.Value = DateSerial(2019, 1, Cell.Value)
                Cell.NumberFormat = "YYYY-MM-DD"
                Application.EnableEvents = True
            End If
        Next Cell
    End If
End Sub

尝试

If Target.value2 > 0 and Target.value2 <= 31 Then
   Target.Value2 = dateSerial(year(now), month(now), Target.Value2)
End If