I have a code that I found on microsoft website :
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
On Error GoTo Err_Work_Days
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt <= EndDate
If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
Exit Function
Err_Work_Days:
' If either BegDate or EndDate is Null, return a zero
' to indicate that no workdays passed between the two dates.
If Err.Number = 94 Then
Work_Days = 0
Exit Function
Else
' If some other error occurs, provide a message.
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function
It works fine, but I would like to get the difference in hours, but it's not working. I Changed the "d" for "h", but instead of giving me the exact hours, it's giving me 24 hours.
When I change "d" for "h" and multiply 1 by 24, and 5 by 24, I dont get the exact difference in hours. This is what I get :
Date1 Date2 DIFFERENCE
2022-05-05 09:05:19; 2022-05-05 15:45:14; 24
it's giving me 24h instead of 6h
The Work_Days function returns the number of weekdays as a whole number. Time is not considered.
This code returns weekday hours between two dates with a time component.
Option Explicit
Function Weekday_Hours(BegDateTime As Variant, EndDateTime As Variant) As Long
' Weekday hours between two dates with a time component
Dim WholeWeeks As Long
Dim DateCurrent As Date
Dim BegDate As Date
Dim EndDate As Date
Dim BegTime As Date
Dim EndTime As Date
Dim EndDays As Long
Dim Week_Days As Long
Dim FirstDay_Hours As Long
Dim LastDay_Hours As Long
Dim FirstDay_Minutes As Long
Dim LastDay_Minutes As Long
' If the weekend, move the start date to Monday 12AM
If Weekday(BegDateTime) = vbSaturday Then ' 7 - Saturday
Debug.Print Weekday(BegDateTime) & ": Saturday"
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
Debug.Print " BegDateTime:" & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
End If
If Weekday(BegDateTime) = vbSunday Then ' 1 - Sunday
Debug.Print Weekday(BegDateTime) & ": Sunday"
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
Debug.Print " BegDateTime:" & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
End If
BegDate = DateValue(BegDateTime)
Debug.Print " BegDate: " & BegDate
EndDate = DateValue(EndDateTime)
Debug.Print " EndDate: " & EndDate
If BegDate >= EndDate Then
Debug.Print "Adjusted BegDate >= EndDate"
Weekday_Hours = 0
Exit Function
Else ' (BegDate < EndDate)
FirstDay_Hours = DateDiff("h", BegDateTime, BegDate + 1)
Debug.Print " FirstDay_Hours: " & FirstDay_Hours
FirstDay_Minutes = DateDiff("n", BegDateTime, BegDate + 1)
Debug.Print "FirstDay_Minutes: " & FirstDay_Minutes
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
BegDate = DateValue(BegDateTime)
End If
EndTime = TimeValue(EndDateTime)
Debug.Print " BegDateTime: " & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
EndDate = DateValue(EndDateTime)
EndTime = TimeValue(EndDateTime)
Debug.Print " EndDateTime: " & EndDate & " " & EndTime
WholeWeeks = DateDiff("w", BegDate, EndDate)
Debug.Print " " & " WholeWeeks: " & WholeWeeks
DateCurrent = DateAdd("ww", WholeWeeks, BegDate)
Debug.Print " DateCurrent: " & DateCurrent
Do While DateCurrent < EndDate
If Weekday(DateCurrent) <> vbSunday And _
Weekday(DateCurrent) <> vbSaturday Then
EndDays = EndDays + 1
Debug.Print " EndDays: " & EndDays
Else
DateCurrent = Format(DateCurrent, "yyyy-mm-dd")
End If
DateCurrent = DateAdd("d", 1, DateCurrent)
Debug.Print " DateCurrent: " & Format(DateCurrent, "yyyy-mm-dd hh:nn:ss")
Loop
Week_Days = WholeWeeks * 5 + EndDays
Debug.Print " Week_Days: " & Week_Days
Debug.Print " Whole day hours: " & Week_Days * 24
BegTime = TimeValue(BegDateTime)
Debug.Print " BegDateTime: " & BegDate & " " & BegTime
LastDay_Hours = DateDiff("h", BegTime, EndTime)
Debug.Print " LastDay_Hours: " & LastDay_Hours
LastDay_Minutes = DateDiff("n", BegTime, EndTime)
Debug.Print " LastDay_Minutes: " & LastDay_Minutes
' Weekday hours
Weekday_Hours = FirstDay_Hours + Week_Days * 24 + LastDay_Hours
Debug.Print " Weekday_Hours: " & Weekday_Hours
Weekday_Hours = (FirstDay_Minutes + LastDay_Minutes) / 60 + Week_Days * 24
Debug.Print " Weekday_Hours: " & Weekday_Hours
End Function
Private Sub Weekday_Hours_test()
Dim BegDateTime As Variant
Dim EndDateTime As Variant
' Whole day hours: 0
' LastDay_Hours: 6
' LastDay_Minutes: 400
' Weekday_Hours: 6 / 7 when using minutes
BegDateTime = "2022-05-05 09:05:19"
EndDateTime = "2022-05-05 15:45:14"
' ?
'BegDateTime = "2022-05-05 15:45:14"
'EndDateTime = "2022-05-05 09:05:19"
' Weekday_Hours: 0
'BegDateTime = "2022-04-30 09:05:19" ' Saturday
'EndDateTime = "2022-05-01 15:45:14" ' Sunday
' FirstDay_Hours: 9
' FirstDay_Minutes: 495
' Whole day hours: 0
' LastDay_Hours: 9
' LastDay_Minutes: 545
' Weekday_Hours: 18 / 17 when using minutes
'BegDateTime = "2022-05-04 15:45:14"
'EndDateTime = "2022-05-05 09:05:19"
' FirstDay_Hours: 15
' FirstDay_Minutes: 895
' Whole day hours: 96
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 126 / 127 when using minutes
'BegDateTime = "2022-05-05 09:05:19"
'EndDateTime = "2022-05-12 15:45:14"
' *** Monday starts at 12AM
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Whole day hours: 48
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 87 / 88 when using minutes)
'BegDateTime = "2022-04-30 09:05:19" ' Saturday
'EndDateTime = "2022-05-05 15:45:14"
' FirstDay_Hours: 15
' FirstDay_Minutes: 895
' Whole day hours: 24
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 54 / 55 when using minutes)
'BegDateTime = "2022-05-05 09:05:19"
'EndDateTime = "2022-05-07 15:45:14" ' Saturday
' *** When time is not entered the default is 12AM ***
' Weekday_Hours: ?
'BegDateTime = "2022-05-05"
'EndDateTime = "2022-05-05"
' One day not two
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Weekday_Hours: 24
'BegDateTime = "2022-05-04"
'EndDateTime = "2022-05-05"
' Three days not four
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Whole day hours: 48
' LastDay_Hours: 0
' LastDay_Minutes: 0
' Weekday_Hours: 72
'BegDateTime = "2022-04-30" ' Saturday
'EndDateTime = "2022-05-05"
If EndDateTime > BegDateTime Then
Debug.Print " Weekday_Hours: " & Weekday_Hours(BegDateTime, EndDateTime)
Else
Debug.Print "?"
End If
End Sub
Related
Based on the code by Rachel Hettinger.
I would like to modify this code in such a way that I have a calendar 2 weeks of 6am to 2:30pm Monday through Friday then Monday through Thursday from 2pm to 12:30am (ending Friday morning). I require it to be this way as my human resources rotate between two weeks days and two weeks nights. I have gotten the calendar to the point that it will apply the two different shift starting times, but not the two different durations, or the non-working Friday afternoon.
I tried an IIF, and tired to have it accomplish this based on the cycle number, but I don't think I have my setup correct. I tried putting the code to select each shift as their own sub, but I'm a beginner and don't think I did it correctly.
Any assistance is appreciated.
'# is the original code
'##add an If qualifier to select between two different set conditions?
' Set e = Iif(CycleNum = "1" Or "2", c.Exceptions.Add(Type:=pjDaily _
' , Start:=CyclesStart + (i - 1) * 7 _
' , Occurrences:=5 _
' , Name:=ExceptionName & CycleNum),
' c.Exceptions.Add(Type:=pjDaily _
' , Start:=CyclesStart + (i - 1) * 7 _
' , Occurrences:=5 _
' , Name:=ExceptionName & CycleNum))
'# Set e = c.Exceptions.Add(Type:=pjDaily _
'# , Start:=CyclesStart + (i - 1) * 7 _
'# , Occurrences:=5 _
'# , Name:=ExceptionName & CycleNum)
'# If Err.Number = 0 Then
'# e.Shift1.Start = StartTime
'# e.Shift1.Finish = StartTime + #8:30:00 AM# '-=adjust length of each shift, #X = hours=-
Else
'switch to day-by-day to not overwrite holidays inherited from the Standard calendar
Dim d As Long
For d = 1 To 5
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 + d - 1 _
, Occurrences:=1 _
, Name:=ExceptionName & CycleNum & " day " & d)
e.Shift1.Start = StartTime
e.Shift1.Finish = StartTime + #6:00:00 AM#
Next d
End If
On Error GoTo 0
Next i
Full Code:
Sub CreateRotatingCalendar()
'ORIGINAL AUTHOR: Rachel Hettinger
' Create a calendar with a cycle of 4 rotating weekly schedules by utilizing the calendar exceptions.
Const CalName As String = "Rotating Shift Alpha" 'name of calendar
Const ExceptionName As String = "Cycle "
'set number of weeks and start times per calendar
Const Week1Start As Date = #6:00:00 AM#
Const Week2Start As Date = #6:00:00 AM#
Const Week3Start As Date = #2:00:00 PM#
Const Week4Start As Date = #2:00:00 PM#
'Set Calendar start and end dates
Const CyclesStart As Date = #8/1/2022# ' must be a Monday
Const CyclesEnd As Date = #8/8/2032# '
' create calendar, but ignore errors in case calendar already exists
On Error Resume Next
BaseCalendarCreate Name:=CalName, FromName:="Standard"
On Error GoTo 0
Dim c As Calendar
Set c = ActiveProject.BaseCalendars(CalName)
' in case this routine is run again to update the rotating calendar, delete exceptions related to the rotation (and leave holidays)
Dim e As Exception
For Each e In c.Exceptions
If e.Name Like ExceptionName & "*" Then
e.Delete
End If
Next e
' remove other shifts and set shift hours
Dim wd As PjWeekday
For wd = pjMonday To pjFriday
c.WeekDays(wd).Shift1.Start = #6:00:00 AM#
c.WeekDays(wd).Shift1.Finish = #2:30:00 PM#
c.WeekDays(wd).Shift2.Start = #2:00:00 PM#
c.WeekDays(wd).Shift2.Finish = #12:30:00 AM#
c.WeekDays(wd).Shift3.Clear
c.WeekDays(wd).Shift4.Clear
c.WeekDays(wd).Shift5.Clear
Next wd
' create the exceptions week by week
Dim NumWeeks As Long
NumWeeks = (CyclesEnd - CyclesStart) \ 7
Dim i As Long
Dim CycleNum As String
Dim StartTime As Date
For i = 1 To NumWeeks + 1
Select Case i Mod 4
Case Is = 1: StartTime = Week1Start: CycleNum = "1"
Case Is = 2: StartTime = Week2Start: CycleNum = "2"
Case Is = 3: StartTime = Week3Start: CycleNum = "3"
Case Is = 0: StartTime = Week4Start: CycleNum = "4"
Case Else
End Select
On Error Resume Next
'##add an If qualifier to select between two different set conditions?
' Set e = Iif(CycleNum = "1" Or "2", c.Exceptions.Add(Type:=pjDaily _
' , Start:=CyclesStart + (i - 1) * 7 _
' , Occurrences:=5 _
' , Name:=ExceptionName & CycleNum),
' c.Exceptions.Add(Type:=pjDaily _
' , Start:=CyclesStart + (i - 1) * 7 _
' , Occurrences:=5 _
' , Name:=ExceptionName & CycleNum))
'# Set e = c.Exceptions.Add(Type:=pjDaily _
'# , Start:=CyclesStart + (i - 1) * 7 _
'# , Occurrences:=5 _
'# , Name:=ExceptionName & CycleNum)
'# If Err.Number = 0 Then
'# e.Shift1.Start = StartTime
'# e.Shift1.Finish = StartTime + #8:30:00 AM# '-=adjust length of each shift, #X = hours=-
Else
'switch to day-by-day to not overwrite holidays inherited from the Standard calendar
Dim d As Long
For d = 1 To 5
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 + d - 1 _
, Occurrences:=1 _
, Name:=ExceptionName & CycleNum & " day " & d)
e.Shift1.Start = StartTime
e.Shift1.Finish = StartTime + #6:00:00 AM#
Next d
End If
On Error GoTo 0
Next i
End Sub
My two added non-functional subs
'# Set Parameters for Shift 1
Sub Day_Shift()
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 _
, Occurrences:=5 _
, Name:=ExceptionName & CycleNum)
If Err.Number = 0 Then
e.Shift1.Start = StartTime
e.Shift1.Finish = StartTime + #8:30:00 AM# '-=adjust length of each shift, #X = hours=-
Else
'switch to day-by-day to not overwrite holidays inherited from the Standard calendar
Dim d As Long
For d = 1 To 5
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 + d - 1 _
, Occurrences:=1 _
, Name:=ExceptionName & CycleNum & " day " & d)
e.Shift1.Start = StartTime
e.Shift1.Finish = StartTime + #6:00:00 AM#
Next d
End If
End Sub
'# Set Parameters for Shift 2
'name
Sub Night_Shift()
'Start day per cycle, how many per week, and name
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 _
, Occurrences:=4 _
, Name:=ExceptionName & CycleNum)
If Err.Number = 0 Then
e.Shift2.Start = StartTime
e.Shift2.Finish = StartTime + #10:30:00 AM# '-=adjust length of each shift, #X = hours=-
Else
'switch to day-by-day to not overwrite holidays inherited from the Standard calendar
Dim d As Long
For d = 1 To 5
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (i - 1) * 7 + d - 1 _
, Occurrences:=1 _
, Name:=ExceptionName & CycleNum & " day " & d)
e.Shift1.Start = StartTime
e.Shift1.Finish = StartTime + #6:00:00 AM#
Next d
End If
End Sub
In this case only one set of exceptions is needed--for the night shifts. However, since those shifts run across midnight the shift needs to be split into 2 parts: 2:00 PM - midnight and midnight - 12:30 AM. This results in the need for 3 exceptions for each of the night shift weeks:
Monday 2:00 PM - 12:00 AM
Tue-Thu 12:00 AM - 12:30 AM and 2:00 PM - 12:00 AM
Friday 12:00 AM - 12:30 AM
Here is updated code that does this:
Sub CreateRotatingCalendar()
' Create a calendar with a cycle of 4 rotating weekly schedules by utilizing the calendar exceptions.
Const CalName As String = "Rotating Shift Alpha" 'name of calendar
Const ExceptionName As String = "Nights "
' Set Calendar start and end dates
Const CyclesStart As Date = #8/1/2022# ' must be a Monday
Const CyclesEnd As Date = #8/8/2032#
' create calendar, but ignore errors in case calendar already exists
On Error Resume Next
BaseCalendarCreate Name:=CalName, FromName:="Standard"
On Error GoTo 0
Dim c As Calendar
Set c = ActiveProject.BaseCalendars(CalName)
' in case this routine is run again to update the rotating calendar, delete exceptions related to the rotation (and leave holidays)
Dim e As Exception
For Each e In c.Exceptions
If e.Name Like ExceptionName & "*" Then
e.Delete
End If
Next e
' remove other shifts and set shift hours
Dim wd As PjWeekday
For wd = pjMonday To pjFriday
c.WeekDays(wd).Shift1.Start = #6:00:00 AM#
c.WeekDays(wd).Shift1.Finish = #2:30:00 PM#
c.WeekDays(wd).Shift2.Clear
c.WeekDays(wd).Shift3.Clear
c.WeekDays(wd).Shift4.Clear
c.WeekDays(wd).Shift5.Clear
Next wd
' create the exceptions week by week
Dim NumWeeks As Long
NumWeeks = (CyclesEnd - CyclesStart) \ 7
Dim WeekNum As Long
For WeekNum = 1 To NumWeeks + 1
If WeekNum Mod 4 = 3 Or WeekNum Mod 4 = 0 Then
' create exceptions for the night shift
On Error Resume Next
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (WeekNum - 1) * 7 _
, Occurrences:=1 _
, Name:=ExceptionName & " Monday")
e.Shift1.Start = #2:00:00 PM#
e.Shift1.Finish = #12:00:00 AM#
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (WeekNum - 1) * 7 + 1 _
, Occurrences:=3 _
, Name:=ExceptionName & " Tu-Th")
e.Shift1.Start = #12:00:00 AM#
e.Shift1.Finish = #12:30:00 AM#
e.Shift2.Start = #2:00:00 PM#
e.Shift2.Finish = #12:00:00 AM#
Set e = c.Exceptions.Add(Type:=pjDaily _
, Start:=CyclesStart + (WeekNum - 1) * 7 + 4 _
, Occurrences:=1 _
, Name:=ExceptionName & " Friday")
e.Shift1.Start = #12:00:00 AM#
e.Shift1.Finish = #12:30:00 AM#
End If
On Error GoTo 0
Next WeekNum
End Sub
Note that since the working hours per week switches between 8.5 hours and 10.5 hours, scheduling should be done in hours rather than days. Review the calendar options for the project and change as needed (File: Options: Schedule)
I am pulling dates from separate sheets but when using if statement the results are wrong but the dates are correct any help?
Dim tacholine, cal_line As Long
Dim tachodate, calstartdate, calenddate As Date
cal_line = 1
'next calender line
cal_line = cal_line + 1
For tacholine = 1 To 842
'check Week 1
tachodate = Format(Worksheets("All Workers").Range("d" & tacholine), "dd/mm/yyyy")
calstartdate = Format(Worksheets("Calender").Range("b" & cal_line), "dd/mm/yyyy")
calenddate = Format(Worksheets("Calender").Range("c" & cal_line), "dd/mm/yyyy")
If tachodate >= calstartdate And tachodate <= calenddate Then
Application.StatusBar = "Match found : " & tachodate & " between dates " & calstartdate & " and " & calenddate
Worksheets("All Workers").Range("u" & tacholine).Value = Worksheets("Calender").Range("A" & cal_line)
End If
Next tacholine
If you are declaring multiple variables on one line, make sure each variable is specifically declared i.e.
Dim tachodate, calstartdate, calenddate As Date
should be
Dim tachodate As Date, calstartdate As Date, calenddate As Date
In VBA, Dim tachodate, calstartdate, calenddate As Date means only calenddate is declared As Date and tachodate, calstartdate are treated as Variant.
May be try
Dim tacholine, cal_line As Long
'declare all variables separately
Dim tachodate As Date, calstartdate As Date, calenddate As Date
cal_line = 1
'next calender line
'cal_line = cal_line + 1 'moved inside loop
For tacholine = 1 To 842
cal_line = cal_line + 1
'check Week 1
tachodate = Format(Worksheets("All Workers").Range("d" & tacholine), "dd/mm/yyyy")
calstartdate = Format(Worksheets("Calender").Range("b" & cal_line), "dd/mm/yyyy")
calenddate = Format(Worksheets("Calender").Range("c" & cal_line), "dd/mm/yyyy")
If tachodate >= calstartdate And tachodate <= calenddate Then
Application.StatusBar = "Match found : " & tachodate & " between dates " & calstartdate & " and " & calenddate
Worksheets("All Workers").Range("u" & tacholine).Value = Worksheets("Calender").Range("A" & cal_line)
End If
Next tacholine
Hi guys i need help to subtract the time. 7:31:52 AM - 4:30:32 the ouput is 3hrs 1 mins 20 sec
Dim date1 = New DateTime(Now.Year, Now.Month, Now.Day, 7, 30, 0, 0)
Dim date2 = DateTime.Parse("4:30:00 PM")
If read1("clog") Is (DBNull.Value) Then
Else
date1 = read1("clog")
Dim duration As Double = (date2 - date1).Minutes
Label4.Text = duration
End If
Dim time1 As DateTime = #7/20/2016 7:31:52 AM#
Dim time2 As DateTime = #7/20/2016 4:30:00 PM#
Dim ts As TimeSpan = time2 - time1
MsgBox(ts.Days & " day(s) " & ts.Hours & " hour(s) " & _
ts.Minutes & " minute(s) " & ts.Seconds & " second(s)")
Dim date1 As DateTime
Dim date2 = DateTime.Parse("4:30:00 PM")
If read1("clog") Is (DBNull.Value) Then
Else
date1 = read1("clog")
Dim duration As TimeSpan = date2 - date1
Label4.Text = String.Format("{0} hour(s) {1} minute(S) {2} second(s)", duration.Hours, duration.Minutes, duration.Seconds
End If
I used below code to be informed if anyone registered in the payroll that he worked less than 30 days. It works great but the issue I have is that the message given contains the name of the employee eg. Nabil Amer worked H days.
Here I want the message to define the number of days instead of the letter "H"
Private Sub Workbook_Open()
On Error Resume Next
Dim EmpName As String
Dim RowNrNumeric As Long
Dim RowNrString As String
Dim CloumnEmpName As String
Dim CloumnNameRemStatus As String
Dim RemStatus As String
Dim Daysworked As String
Sheets("Master Payroll").Select
CloumnEmpName = "C"
CloumnNameRemStatus = "K"
Daysworked = "H"
RowNrNumeric = 2
RowNrString = RowNrNumeric
EmpName = Range(CloumnEmpName + RowNrString).Value
DueDate = Range(CloumnNameDate + RowNrString).Value
RemStatus = Range(CloumnNameRemStatus + RowNrString).Value
Do While EmpName <> ""
If Cells(RowNrNumeric, 8).Value < 30 _
And Not IsEmpty(Cells(RowNrNumeric, 2)) Then
MsgBox "WARNING: " + EmpName + " worked for " + Daysworked + " Days "
Range(Daysworked + RowNrString).Interior.ColorIndex = 3
Range(Daysworked + RowNrString).Select
End If
RowNrNumeric = RowNrNumeric + 1
RowNrString = RowNrNumeric
EmpName = Range(CloumnEmpName + RowNrString).Value
RemStatus = Range(CloumnNameRemStatus + RowNrString).Value
Loop
End Sub
Screen shot:
Change this:
MsgBox "WARNING: " + EmpName + " worked for " + Daysworked + " Days "
To this:
MsgBox "WARNING: " + EmpName + " worked for " & Range(Daysworked & RowNrNumeric).Value & " Days "
I feel like this should be possible with less lines. Am I missing a VBA function to make this easier?
Sub dates()
Dim sDay As String
Dim eday As String
Quarter = DatePart(q, Date)
If Quarter = 1 Then
sDay = "1/1/" & DatePart("yyyy", Date)
eday = "3/31/" & DatePart("yyyy", Date)
ElseIf Quarter = 2 Then
sDay = "4/1/" & DatePart("yyyy", Date)
eday = "6/30/" & DatePart("yyyy", Date)
ElseIf Quarter = 3 Then
sDay = "7/1/" & DatePart("yyyy", Date)
eday = "9/30/" & DatePart("yyyy", Date)
ElseIf Quarter = 4 Then
sDay = "10/1/" & DatePart("yyyy", Date)
eday = "12/31/" & DatePart("yyyy", Date)
End If
End Sub
Yep:
Sub dates()
Dim sDay As String
Dim eDay As String
Dim Quarter As Integer
For Quarter = 1 To 4
sDay = CStr(3 * (Quarter - 1) + 1) & "/1/" & DatePart("yyyy", Date)
eDay = DateAdd("d", -1, DateAdd("q", 1, CDate(sDay)))
Debug.Print "Quarter : " & Quarter & vbTab & sDay & vbTab & eDay
Next
End Sub
Also note that in your code, q needs to be offset in double-quotes as a string literal, unless it is defined elsewhere as such.
Quarter = DatePart("q", Date)
Sub Qmonth()
Dim CMonth As Integer 'selected month from range
Dim CYear As Integer 'selected year from range
CMonth = Range("MonthO").Value
CYear = Range("YearO").Value
Select Case CMonth
Case 1 To 3
Debug.Print DateSerial(CYear, 1, 1)
Case 4 To 6
Debug.Print DateSerial(CYear, 4, 1)
Case 7 To 9
Debug.Print DateSerial(CYear, 7, 1)
Case 10 To 12
Debug.Print DateSerial(CYear, 10, 1)
End Select
End Sub