Reputation: 83
I'm using Excel VBA, How can I ?
Input month and year and return all week numbers for this month.
Input a date and get the week number for this date.
Upvotes: 0
Views: 6365
Reputation: 939
Input a date and get the week number for this date.
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
Input month and year and return all week numbers for this month.
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
, so on February of 2017 it will output 28
.MonthYearDay
and populate the array MyArray
with the week of that day Application.WorksheetFunction.WeekNum(MonthYearDay)
.arr(i)
to output the unique values from the array MyArray
On future requests, post what you have tried. I am answering it because i couldn't find a suitable answer for: Input month and year and return all week numbers for this month.
Upvotes: 2