简体   繁体   English

从Excel中每个会计年度的第一个星期日开始周数

[英]Start the Week Number from the first sunday of every fiscal year in Excel

I am trying to find the week number for a fiscal year which is starts on first sunday of February. 我试图找到从2月的第一个星期日开始的会计年度的周数。 I have got it to a point where I can get the week number which starts on first of every year (in my case feb). 我已经达到了这样的程度,我可以获得每年第一天开始的周数(在我的情况下是feb)。

Not able to start it from First Sunday. 无法从第一个星期日开始。 Below is what I've come up with. 以下是我的想法。

=IF(AND(MONTH($E2)=2,DAY($E2)=1),1,ROUNDUP(($E2-DATE(YEAR($E2)-IF(MONTH($E2)<2,1,0),2,0)+WEEKDAY(DATE(YEAR($E2)-IF(MONTH($E2)<2,1,0),2,0)))/7,0))

I would also like it to end on Saturday of last week of the year. 我希望它能在今年上周六的星期六结束。 For example: in Feb 2016, the week count should start from 7thFeb2016 and the count should end on 4thFeb2017. 例如:2016年2月,周计数应从2016年第7季度开始,计数应于2017年2月2日结束。

Use this formula to find week number of given date: 使用此公式查找给定日期的周数:

=IF(A1>=IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1),2,1),DATE(YEAR(A1),2,7-WEEKDAY(DATE(YEAR(A1),2,1),1)+2)),ROUNDUP((A1-IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1),2,1),DATE(YEAR(A1),2,7-WEEKDAY(DATE(YEAR(A1),2,1),1)+2))+1)/7,0),ROUNDUP((A1-IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1)-1,2,1),DATE(YEAR(A1)-1,2,7-WEEKDAY(DATE(YEAR(A1)-1,2,1),1)+2))+1)/7,0))

I hope you want this. 我希望你想要这个。

UPDATED 更新

If you want week number of fiscal quarter, use this: 如果您想要财政季度的周数,请使用:

=ROUNDUP(MOD(=IF(A1>=IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1),2,1),DATE(YEAR(A1),2,7-WEEKDAY(DATE(YEAR(A1),2,1),1)+2)),ROUNDUP((A1-IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1),2,1),DATE(YEAR(A1),2,7-WEEKDAY(DATE(YEAR(A1),2,1),1)+2))+1)/7,0),ROUNDUP((A1-IF(WEEKDAY(DATE(YEAR(A1),2,1),1)=1,DATE(YEAR(A1)-1,2,1),DATE(YEAR(A1)-1,2,7-WEEKDAY(DATE(YEAR(A1)-1,2,1),1)+2))+1)/7,0)),13.01),0)

I just chucked this together (no doubt there is a more elegant way to do this). 我只是把它们放在一起(毫无疑问,有一种更优雅的方式可以做到这一点)。 You can use it by putting it into a Module in your VBA editor and then on the worksheet use the function =AltWeekNumber(E2) 您可以通过将其放入VBA编辑器中的Module中然后在工作表上使用函数=AltWeekNumber(E2)

Function AltWeekNumber(endDate As Range) As Date
    Dim startDate As Date
    startDate = FirstFebruarySunday(year(endDate.Value))
    If startDate > endDate Then
        startDate = FirstFebruarySunday(year(endDate.Value) - 1)
    End If
    AltWeekNumber = DateDiff("w", startDate, endDate.Value, vbSunday)
End Function

Private Function FirstFebruarySunday(year As Integer) As Date
    For i = 1 To 7
        If Weekday(DateSerial(year, 2, i)) = 1 Then
            FirstFebruarySunday = DateSerial(year, 2, i)
            Exit Function
        End If
    Next i
End Function

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

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