简体   繁体   English

Excel公式-> VBA:当月每个星期五的日期

[英]Excel Formula -> VBA: Date of each Friday of the current month

I have the current formula created by @ScottCraner which when pasted into Cell Q8 and Dragged down to Q12, populates the cells with the date of each friday this month: 我有@ScottCraner创建的当前公式,将其粘贴到单元格Q8中并拖到第12季度时,将使用本月每个星期五的日期填充单元格:

Code: 码:

=IFERROR(AGGREGATE(15,6,ROW(INDEX(A:A,EOMONTH(TODAY(),-1)+1):INDEX(A:A,EOMONTH(TODAY(),0)))/(WEEKDAY(ROW(INDEX(A:A,EOMONTH(TODAY(),-1)+1):INDEX(A:A,EOMONTH(TODAY(),0))),1)=6),ROW(1:1)),"-")

Im trying to convert this into VBA as i understand VBA more than formulas. 我试图将其转换为VBA,因为我对VBA的了解不止于公式。 However was wondering if anyone here could possibly help. 但是,我想知道这里是否有人可以提供帮助。

It really is appreciated 真的很感激

Try this quick UDF. 试试这个快速的UDF。

Function listWeekday(dt As Long, ndx As Long, _
                     Optional wd As Long = 6)
    listWeekday = 7 - Weekday(DateSerial(Year(dt), Month(dt), 0), wd) + _
                  DateSerial(Year(dt), Month(dt), 1) + _
                  (ndx - 1) * 7
    If Month(dt) <> Month(listWeekday) Then _
        listWeekday = CVErr(xlErrNA)

End Function

'usage for Fridays in current month
=listWeekday(today(), row(1:1))
'usage for Sundays in current month
=listWeekday(today(), row(1:1), 1)
'usage for Wednesdays in current month
=listWeekday(today(), row(1:1), 4)

This iterates the dates and puts the Fridays in Q8:Q12 这会迭代日期,并将星期五放在Q8:Q12

Sub myFri()
    Dim OArr(1 To 5, 1 To 1) As Variant
    Dim k As Long
    k = 1
    Dim i As Long
    For i = DateSerial(Year(Date), Month(Date), 1) To DateSerial(Year(Date), Month(Date) + 1, 0)
        If Weekday(i, vbSunday) = 7 Then
            OArr(k, 1) = i
            k = k + 1
        End If
    Next i

    If k = 5 Then OArr(k, 1) = "-"

    Worksheets("Sheet1").Range("Q8:Q12").Value = OArr
    Worksheets("Sheet1").Range("Q8:Q12").NumberFormat = "mm/dd/yyyy"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM