Employee Date and Time Off Request (VBA)

esneaker

New Member
Joined
Feb 1, 2015
Messages
11
Greetings All!!

Introduction:
I hope all is well. I am attempting to create a system where the user can log which date and time they will not be available to come into work. The day is a 24hr day, and there are 4 shifts within each day (presumably 6hr increments). I just want the user to put in the date and the time, and the system should (in a Listbox) print the Date their leave started and the the block associated with their time...as well as the date/block of their end: Example:

Blocks A(6am-12pm), B(12pm-6pm), C(6pm-12am), D(12am-6am)for each day. If they will not be available from 5am(Day 1) through 7pm(Day 3)...the results should be:

Day0(BlockD)- Day3(BlockC).

Below is my code. I'm using comboBoxes (time, day,month,year) for the start and finish of their leave period entries, and the two dictionary objects that reference the cells where the work hour blocks are determined. My problem isn't that the code does not iterate through the work hours...it's that it will count ALL of the times that are in that work block and NOT the blocks that they are requesting. For instance with the example above. The result of my code will produce: Day1A, Day1B, Day1C, Day1D....(through day 2)....Day3C, Day3D. Obviously, I don't want this.

Private Sub request()
mStart = monthStart.Value
dStart = dayStart.Value
yStart = yearStart.Value
startDate = mOut & " " & dOut & ", " & yOut 'String Value to compare with Dictionary
myStart = CDate(startDate)
mFinish = monthFinsh.Value
dFinish = dayFinish.Value
yFinish = yearFinish.Value
endDate = mYn & " " & dIn & ", " & yIn
myEnd = CDate(endDate)
days = optempo(CDate(startDate), CDate(endDate))
mTO = TimeValue(timeStart.Value)
mTI = TimeValue(timeEnd.Value)

Set workdate = CreateObject("scripting.dictionary")
workdate.Item(Sheets("HRTime").Range("B3")) = "1"
workdate.Item(Sheets("HRTime").Range("B4")) = "2"
workdate.Item(Sheets("HRTime").Range("B5")) = "3"
workdate.Item(Sheets("HRTime").Range("B6")) = "4"
workdate.Item(Sheets("HRTime").Range("B7")) = " 5"
workdate.Item(Sheets("HRTime").Range("B8")) = " 6"
workdate.Item(Sheets("HRTime").Range("B9")) = " 7"
workdate.Item(Sheets("HRTime").Range("B10")) = " 8"
workdate.Item(Sheets("HRTime").Range("B11")) = " 9"
workdate.Item(Sheets("HRTime").Range("B12")) = " 10"
workdate.Item(Sheets("HRTime").Range("B13")) = " 11"
workdate.Item(Sheets("HRTime").Range("B14")) = " 12"
workdate.Item(Sheets("HRTime").Range("B15")) = " 14"
workdate.Item(Sheets("HRTime").Range("B16")) = " 15"
Set worktime = CreateObject("scripting.dictionary")
worktime.Item(Sheets("HRTime").Range("C20")) = "A"
worktime.Item(Sheets("HRTime").Range("C21")) = "B"
worktime.Item(Sheets("HRTime").Range("C22")) = "C"
worktime.Item(Sheets("HRTime").Range("C23")) = "D"


Dim k As Long
For Each i In workdate.keys
For Each j In worktime.keys
If CDate(i.Value) >= myStart And CDate(i.Value) <= myEnd Then
If TimeValue(j) >= mTO and TimeValue(j) <= mTI Then
myLeave = workdate.Item(i) & worktime.Item(j)

'LeaveBox is a List Box to populate with the values from search
LeaveBox.AddItem (myLeave)

Else
MsgBox "NO workdays fall within your requested leave period."
End If

End If
Next
Next
End Sub

I hope that I'm not confusing anyone...but I'm been mashing my head against the desk for 3days. Any help would be greatly appreciated!!! Even if its simplifying/refactoring the lines! Thank you very much for your expertise and advice!
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top