使用日期作为参数创建一个月中的几天

Create a days of month using a date as parameter

我在单元格 A1 中有一个日期,例如:12/08/22

我想使用单元格 A1monthyearE 列中创建一个包含一个月中所有日期 (1-31) 的列表参数.

Sub TESTEEEEEEE()

    Dim r As Range, i As String, j As Long
     i = Range("A1").Offset(0, 1).Value = Left(cell.Value, 3)
    'k = ????
    j = 31
    For Each r In Range("E1:E31")
        r.NumberFormat = "@"
        r.Value = Format(DateSerial(k, i, j), "dd/m/yy")
         j = j + 1
    Next r
End Sub

我被困在如何提取月份和年份的问题上。我正在尝试使用字符的位置作为参数,但我没有让它工作。

i 应该提取返回 08 的 4,5 和字符(好的,代码是错误的,我正在做一些测试)。

k 应该提取返回 22.

的 7,8 字符

有人可以帮助我吗?

您可能正在使用 YEAR, MONTH, EOMONTH

等日历功能
Sub DebugDate()

    Dim rg As Range
    Set rg = Range("A1")  ' should contain a date
    Dim dt As Date
    dt = rg.Value

    Debug.Print Year(dt), Month(dt), CDate(WorksheetFunction.EoMonth(dt, 0))
    ' End of month not using worksheet function EOMONTH
    Debug.Print DateSerial(Year(dt), Month(dt) + 1, 1) - 1

End Sub

进一步阅读How to create a calendar with VBA

请尝试使用下一种方式。它不需要迭代:

Sub testCreateWholeMonth()
   Dim D As Date, lastD As Long
   D = Range("A1").value
   
   lastD = Day(WorksheetFunction.EoMonth(DateSerial(Year(D), Month(D), 1), 0))
   With Range("E1:E" & lastD)
        .value = Evaluate("date(" & Year(D) & "," & Month(D) & ",row(1:" & lastD & "))")
        .NumberFormat = "mm.dd.yyyy"
    End With
End Sub