[英]excel formula for the week according to the start date
我想根据数据的开始日期,用excel公式填写“周”列。 请解。
第 1 周从第 2 行开始,每 7 行计数一次。
谢谢
日期 | 星期 |
---|---|
2022 年 1 月 12 日 | 第一周 |
02/12/2022 | 第一周 |
03/12/2022 | 第一周 |
2022 年 4 月 12 日 | 第一周 |
06/12/2022 | 第一周 |
2022 年 7 月 12 日 | 第一周 |
08/12/2022 | 第 2 周 |
2022 年 9 月 12 日 | 第 2 周 |
2022 年 10 月 12 日 | 第 2 周 |
11/12/2022 | 第 2 周 |
12/12/2022 | 第 2 周 |
13/12/2022 | 第 2 周 |
14/12/2022 | 第 2 周 |
15/12/2022 | 第 3 周 |
据我了解,您想用 WeekNum() 值填充该列,对吗? 然后:
Range('B2:B16').FormulaR1C1 = '=Weeknum(Offset(R[0]C[0],0,-1))'
编辑:我想你的意思是你想枚举开始的第 2 行并在前面添加文本“Week”:
Range('B2:B16').FormulaR1C1 = '= "Week " & TEXT(CEILING.MATH( (ROW()-1)/7),"00")'
EDIT2:兼容 Excel 2010 版本(您应该从一开始就说过):
Range('B2:B16').Formula = '= "Week " & TEXT(CEILING( (ROW()-1)/7,1),"00")'
我认为 Anders Balari 是对的。 通常=weeknum(A1)
的答案是第 49 周。您想从中减去 (49-1) 48 周,这样您就可以在 12 月 1 日“开始”第 1 周。 如果你想要那样,你应该使用如下代码创建一个函数:
Sub week()
Dim intWeek As Integer
Dim intCounter, intCounterNew As Integer
Dim intLastRow As Integer
Dim wsData As Worksheet
'assuming the dates are in column 1 starting in row 1
Set wsData = ThisWorkbook.ActiveSheet
intLastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
intCounterNew = 1
For intCounter = 1 To intLastRow
intWeek = WorksheetFunction.WeekNum(wsData.Cells(intCounter, 1))
If intCounter <> 1 Then
If intWeek = WorksheetFunction.WeekNum(wsData.Cells(intCounter - 1, 1)) Then
wsData.Cells(intCounter, 2) = "week " & intWeek - (intWeek - intCounterNew)
Else
intCounterNew = intCounterNew + 1
wsData.Cells(intCounter, 2) = "week " & intWeek - (intWeek - intCounterNew)
End If
Else
wsData.Cells(1, 2) = "week " & intWeek - (intWeek - intCounterNew)
End If
Next intCounter
End Sub
请注意,我没有执行任何功能,而是执行了一个遍历日期集的宏。 我试过了,它奏效了。
我不知道如何创建这样的函数。
好吧:我想我明白了。 这段代码:
Sub CustomWeeks()
'assuming the dates are in column 1 starting in row 1
Dim wsData As Worksheet
Dim intCounter, intLastRow, intWeeknumber, intDayCounter As Integer
Set wsData = ActiveSheet
intLastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
intWeeknumber = 1
intDayCounter = 1
For intCounter = 1 To intLastRow
If intCounter = 1 Then
wsData.Cells(intCounter, 2) = "Week " & intWeeknumber
intDayCounter = intDayCounter + 1
Else
If intDayCounter > 7 Then
intDayCounter = 1
intWeeknumber = intWeeknumber + 1
wsData.Cells(intCounter, 2) = "Week " & intWeeknumber
intDayCounter = intDayCounter + 1
Else
wsData.Cells(intCounter, 2) = "Week " & intWeeknumber
intDayCounter = intDayCounter + 1
End If
End If
Next intCounter
End Sub
结果如你所愿。 所以:在第一天,第一周将开始,并且将是第 1 周 7 天。 之后进入第 2 周,依此类推。 你是这个意思吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.