【问题标题】:List all days in a row VBA EXCEL连续列出所有日期 VBA EXCEL
【发布时间】:2019-11-27 22:38:08
【问题描述】:

我有一个年份列表(例如 2016、2017、2018、2019、2020、2021...)

如何列出 2020 年的所有日子?

所以我会: .Cells(2, 2) = 01/01/2020.... 等到 2020 年 12 月 31 日显然在同一行

这就是我的代码的样子,但我认为我让它只适用于当年,例如。 2019

Dim MyMounth As Long
Dim MounthDays As Long
Dim MounthDays2 As Long
Dim MounthDaysAppoggio As Long
Dim riga As Long
Dim numeri As Long
Dim mese As Long

MounthDaysAppoggio = 1
MounthDays = Day(DateSerial(Year(Date), MounthDaysAppoggio + 1, 1) - 1)

mese = 1
riga = 2
numeri = 1

MounthDays2 = 0

For q = 1 To 12

For Row = 2 To MounthDays + 1

MounthDays2 = MounthDays2 + 1
Cells(4, riga).NumberFormat = 0
Cells(4, riga).Value = MounthDays2
Cells(4, riga).VerticalAlignment = xlCenter
Cells(4, riga).HorizontalAlignment = xlCenter
Cells(4, riga).BorderAround ColorIndex:=1, Weight:=xlMedium

riga = riga + 1

Next Row

MounthDays = Day(DateSerial(Year(Date), MounthDaysAppoggio + 1, 1) - 1)

MounthDays2 = 0

Next q

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这个简单的宏还可以让您选择时间段:

    Option Explicit
    Sub Test()
    
        'First day to the time period
        Dim FirstDay As Date
        FirstDay = DateSerial(2020, 1, 1)
    
        'last day to the time period
        Dim LastDay As Date
        LastDay = DateSerial(2020, 12, 31)
    
        'count the days between first and last day
        Dim Counter As Long
        Counter = DateDiff("d", FirstDay, LastDay)
    
        'Resize an array as large as the days you neeed
        ReDim arr(0 To Counter + 1) As Date
    
        'loop through the array with the DateSerial function adding 1 day to the first day of the period
        Dim i As Long
        For i = 0 To UBound(arr)
            arr(i) = DateSerial(2020, 1, 1 + i)
        Next i
    
        'Drop the array to your desired row
        Range("A1").Resize(1, UBound(arr)).Value = arr
    
    End Sub
    

    【讨论】:

    • 感谢您的回答,如果我问,对不起,但如果我只有一个输入,只有 2020 年。我怎么能做到这一点?
    • @TheMite 我的代码完成了整个 2020 年,您只需编辑 FirstDay =....LastDay = ... 以满足您的需求。现在是 2020 年。
    • 是的,对不起,达米安,我的问题错了,真的很抱歉,它是 2020 年。我问的是年份列表,比如用户从列表中选择 2022 年,我如何获得 FirstDay 和 LastDay?希望你能理解我
    • 我发现了!谢谢达米安不需要更多的答案
    • @TheMite 很高兴你发现了方法。您只需要将 2020 转换为变量,并将其作为参数传递给过程即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2020-04-05
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多