Please could you assist me to add Week number for particular months?
This the vba:
With Target
If .Column <> 10 Or .Row < 1 Then Exit Sub
If .Value = "Select" Then
If .Offset(0, 1).Value = "" Then
.Offset(0, 1).NumberFormat = "mm/dd/yy"
.Offset(0, 1).Value = Now - 1
End If
My definition of week number is based on full weeks being Sunday through to Saturday:
If the first of the month does not start on a Sunday then any days up to the first Saturday of the month will be Week 1
The next sets of Sunday through Saturday will be Week 2, Week 3, Week 4 etc. Unless the month started on a Sunday in which case the sets will be Week 1, Week 2, Week 3 etc.
If the month does not end on a Saturday, then the Sunday through to till the end of the month will be Week N+1 where N is the last full week given by step 2.
For example: this month, the 1st of March is on Wednesday. So March 1st-4th (Wed-Sat) will be 1 week and so on.
You can try and see if the WeekNum function suits your purpose.
You can use it in your code like this:
.Offset(0, 2).Value = WorksheetFunction.WeekNum(Now - 1)
The documentation says:
Returns the week number of a specific date. For example, the week containing January 1 is the first week of the year, and is numbered week 1.
There are two systems used for this function:
System 1 The week containing January 1 is the first week of the year, and is numbered week 1.
System 2 The week containing the first Thursday of the year is the first week of the year, and is numbered as week 1. This system is the methodology specified in ISO 8601, which is commonly known as the European week numbering system.
Edit
OP definition of week is per a desktop calendar where each green block is a week - so March 2017 has 5 weeks. Note January 2016 has six weeks under this system!
Therefore, the WeekNum formula will not give the expected result. Instead the following function can be used:
Function GetCalendarTypeMonthWeek(dt As Date) As String
Dim lngDayOfMonth As Long
Dim lngWeekDay As Long
Dim dtFirstDayOfMonth As Date
Dim lngFactor As Long
lngDayOfMonth = Day(dt)
lngWeekDay = Weekday(dt, vbSunday) '<~~ Sunday=1, Monday=2, etc
'does month start on Sunday?
dtFirstDayOfMonth = DateValue("01-" & Month(dt) & "-" & Year(dt))
If Weekday(dtFirstDayOfMonth, vbSunday) = 1 Then
lngFactor = 1
Else
lngFactor = 2
End If
'get calendar week number for date
GetCalendarTypeMonthWeek = "Week " & CStr(Int((lngDayOfMonth - lngWeekDay) / 7) + lngFactor)
End Function
To be used in the sample code like:
.Offset(0, 2).Value = GetMonthWeek(Now - 1)
I think the next function will return the value you want:
Function WeekOfTheMonth(DateRef As Date) As Integer
Dim WeekFirstDayRefMonth As Integer
WeekFirstDayRefMonth = Application.WeekNum(DateSerial(Year(DateRef), Month(DateRef), 1), 2)
Dim WeekLastDayRefMonthB As Integer
WeekLastDayRefMonthB = Application.WeekNum(DateSerial(Year(DateRef), Month(DateRef), 1) - 1, 2)
Select Case WeekFirstDayRefMonth - WeekLastDayRefMonthB
Case 0: WeekOfTheMonth = Application.WeekNum(DateRef, 2) - WeekLastDayRefMonthB + 1
Case 1: WeekOfTheMonth = Application.WeekNum(DateRef, 2) - WeekLastDayRefMonthB
Case Else: WeekOfTheMonth = Application.WeekNum(DateRef, 2)
End Select
End Function
Just write WeekOfTheMonth("place your date here") on your code and you're good to go.
Please note: I didn't check all the scenarios for this code so please let me know if you're getting unexpected results.
Related
I have a few dates within a row (ex. 4/30/2020, 5/31/2020, 6/30/2020) and am trying to add the next end of month date to each subsequent cell. Ex. after 6/30/2020, I want to add 7/31/2020 within the following column (same row). I have been able to add the correct months and years using the DateAdd fxn but the days of the months have been off. For example, all of the days are 30 when some should be 31 or 29/28. I am doing this for 3 months at a time using a code that is selecting the date of the last cell in the "date" row.
With Sheets("Sheet1")
lastCol = .Cells(6, .Columns.Count).End(xlToLeft).Column
dDate = .Cells(6, lastCol).Value
End With
Dim y As Integer
For y = 0 To 2
Cells(6, lastCol + y).Select
Selection.Offset(0, 1).Select
Selection.Value = DateAdd("m", 1 + y, dDate)
Selection.Font.Bold = True
Next y
What am I doing wrong??
Thanks!
I developed the vba code below to calculate cumulative production up to one date in the column "K" by adding amounts in the column "I" corresponding to dates less than the one in the column "K"
Option Explicit
Sub Gp()
Dim date2 As Double 'Same p/z production Dates'
Dim Gp As Double 'Cumulative production volumes"
Dim fraction As Double
Dim Daysmonth As Double
Dim Days As Double
Dim i As Integer, j As Integer
Gp = 0
j = 3
For i = 4 To 43
date2 = Cells(i, "K")
Do While (Cells(j, "A") < date2)
Gp = Gp + Cells(j, "I")
j = j + 1
Loop
Daysmonth = Cells(j, "A") - Cells(j - 1, "A")
Days = date2 - Cells(j - 1, "A")
fraction = Cells(j, "I") * Days / Daysmonth
Gp = Gp + fraction
Cells(i, "M") = Gp
Gp = (Gp + Cells(j, "I")) - fraction
j = j + 1
Next i
End Sub
This code takes the first cell of the "K" column and look all the dates less than date in the "A" column. Then it add all the amounts in the "I" column for those corresponding dates. Then if the date in the column "A" is in the same month and year of the date in the column "K" it will calculate the fraction of the amount corresponding to those days. Everything is alright until 8/14/80 when calculating the fraction it gives -17 instead of 14 days for 8/14/80 - 8/31/80. It gives me 6304593 and it should give me 6350518.484.
This is a picture of my answers in the column "M" and the right answers in the column "N". They are very similar but not the same for some dates
Calculated cumulative and right answers next to it
"when calculating the fraction it gives -17 instead of 14 days for 8/14/80 - 8/31/80"
Not sure where you are getting the 14 days. Since 8/31/80 is in the future it would be 17 days after 8/14/80. Based upon your formula of 8/14/80 - 8/31/80, it arrives at -17 which is correct.
If you are okay to include dates greater than 'date2', you could use the built in 'Abs()' function to always make the result positive and that should correct the calculation.
Days = Abs(date2 - Cells(j - 1, "A"))
Output of 8/14/80 - 8/31/80 should be -17 only , instead of 14 days. Just to check, I have tried your code and replaced the value -17 with 14 then output i am getting is 7255151.33 instead of 6350518.484.
I think fraction should not be calculated in this case(8/14/80 - 8/31/80) as the primary condition is date in column K(8/14/80) should be greater than Date in column A(8/31/80) which is failing in this case.
And if it is required then you can check if the output of date2 - Cells(j - 1, "A") is negative then replace it with Cells(j - 1, "A")-date2 to make the result positive.
In Excel, I'd like a list of the 1st and 3rd Mondays in each month, for say the next six months after today.
Using a formula-based approach, I can find the 1st or 3rd Monday after today. I got stuck when trying to populate the list after that. Particularly, a challenge is to account for months with 5 Mondays in them. The formulas get very complex very quickly!
This is the solution I have so far:
Private Sub Auto_Open()
Dim i, mday As Integer
Dim row, col As Integer
Dim d As Date
Dim min As Integer
min = 14 'Minimum set up period
col = 1 'Column to put results in
row = 2 'Row to start from
d = Date + min + 9 - Weekday(Date) 'Find Monday after Minimum set up period
Worksheets("options").Activate
For i = 1 To 26 'Results up to 26 weeks in future
mday = day(d)
If (mday - 7 <= 0) Then 'First week
Cells(row, col).Value = d
row = row + 1
ElseIf (mday - 14 > 0 And mday - 21 <= 0) Then 'Third week
Cells(row, col).Value = d
row = row + 1
End If
d = d + 7 '+1 week
Next
Worksheets("New Starter").Activate
End Sub
Try this:
On A1 place the first day of one month. On A2 place: =EOMONTH(A1,0)+1
On B1 place =A1+MOD(8-WEEKDAY(A22,2),7).
On C1 place =B1+14
Drag an drop down.
The first column is taking the first day of one month and consecutive month, the column Bis returning the first monday and colunm C is returning the third monday of that month.
If you are looking for the 5th monday:
On D1 place: =IF(A1+MOD(8-WEEKDAY(A22,2),7)+28<EOMONTH(A1,0)+1,A1+MOD(8-WEEKDAY(A22,2),7)+28,"")
Below code hide a certain number of rows(depending on the number of the week we are in) and shows only the cell corresponding to the current week number and cells corresponding to the future week numbers.
Dim test As String
test = Format(Now, "yyyy", vbMonday) & KW(Now)
For k = 3 To lastColumn
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).ColumnWidth = cWidth
If ThisWorkbook.Worksheets(PlanningTableNameUG).Cells(1, k).Value = test Then
today = True
On Error Resume Next
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k - 1).Ungroup
On Error GoTo 0
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k - 1).Group
End If
If Not today Then
On Error Resume Next
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Ungroup
On Error GoTo 0
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Group
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Hidden = True
If Hidden = True Then
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Group.Copy
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Group.Insert Shift:=xlToRight
End If
Else
ThisWorkbook.Worksheets(PlanningTableNameUG).Columns(k).Hidden = False
End If
Next k
' calculate the week number
Function KW(d As Date) As Integer
Dim Tag As Long
Tag = DateSerial(Year(d + (8 - Weekday(d)) Mod 7 - 3), 1, 1)
KW = (d - Tag - 3 + (Weekday(Tag) + 1) Mod 7) \ 7 + 1
End Function
Now, I need to count how many columns were hidden and add the exact numbers of columns, example:
columns:
1,2,3,4,5,6,7,8,9,10 (column 4 is corresponding to today week number)
I hide: 1,2,3 because there are in the past weeks, and want to add 11,12, 13, together with the week number corresponding for them, but not more than 1 year from current date.
Count of the weeks starts at the begging of the current year.
When hiding the columns you could add an integer variable that increases by one then use this variable to add that many new columns.
Then to add the week number, use the last column with the week number on it and add one to it for each of the new columns...
To count hidden columns in used range of ThisWorkbook.Worksheets(PlanningTableNameUG):
Dim col As Range
Dim cnt As Long
For Each col In ThisWorkbook.Worksheets(PlanningTableNameUG).UsedRange
Debug.Print col.EntireColumn.Hidden
cnt = cnt - col.EntireColumn.Hidden
Next col
EntireColumn.Hidden returns True if is hidden. True is -1 in VBA, that is why i deduct it from the counter to get positive value.
How do I get continued list of months for specific number of year?
e.g. if I entered 5 years, starts from January 2014 then below list will come which have January 2014 to December 2014 then again January 2015 to December 2015...till December 2019.
How can I get this any one have idea?
Enter the number of years in cell B1 and run this small macro:
Sub MonthMaker()
nYears = [b1]
k = 1
For i = 1 To nYears
For j = 1 To 12
Cells(k, 1) = DateSerial(2013 + i, j, 1)
Cells(k, 1).NumberFormat = "mmmm yyyy"
k = k + 1
Next j
Next i
End Sub
and if you do not wish to use VBA then enter the number of years in B1 and in A1 enter:
=IF(ROUNDDOWN((ROW()-1)/12,0)>$B$1-1,"",DATE(2014,ROW(),1))
and copy down.