[英]Excel VBA : week numbers
我正在使用Excel VBA,该怎么办?
输入月份和年份,并返回该月的所有星期数。
输入日期并获取该日期的星期数。
输入日期并获取该日期的星期数。
Sub WeekDate()
Dim InputDate As Date
Dim week As Long
InputDate = Now 'Change to desired date/cell
week = Application.WorksheetFunction.WeekNum(InputDate)
Debug.Print week
End Sub
输入月份和年份,并返回该月的所有星期数。
Sub WeeksInMonth()
Dim MonthYear As String, txt As String
Dim InputDate As Date, MonthYearDay As Date
Dim i As Long, intDaysInMonth As Long, j As Long
Dim MyArray As Variant
Dim arr As New Collection, a
ReDim MyArray(0 To 31)
j = 0
InputDate = Now
MonthYear = Month(InputDate) & "/" & Year(InputDate)
intDaysInMonth = Day(DateSerial(Year(MonthYear), Month(MonthYear) + 1, 0))
For i = 1 To intDaysInMonth
MonthYearDay = DateSerial(Year(InputDate), Month(InputDate), i)
MyArray(j) = Application.WorksheetFunction.WeekNum(MonthYearDay)
j = j + 1
Next i
ReDim Preserve MyArray(0 To j - 1)
On Error Resume Next
For Each a In MyArray
arr.Add a, CStr(a)
Next
For i = 1 To arr.Count
Debug.Print arr(i)
Next
End Sub
intDaysInMonth
获取一个月中的所有日期,因此在2017年2月它将输出28
。 MonthYearDay
在每月的每一天循环,并用当天的星期几填充数组MyArray
Application.WorksheetFunction.WeekNum(MonthYearDay)
。 arr(i)
以从数组MyArray
输出唯一值 在将来的请求中,发布您尝试过的内容。 我之所以回答,是因为我找不到适合的答案:输入月份和年份并返回该月的所有星期数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.