File userform listboxes with values from sheet based on cell value - excel

VB noob. I am trying to fill userform list boxes with all times where the reservations column isn't blank, for each room. (If Reservations is blank, it's not a reservation made in advance).
Sample data:
date room intime outime Reservations
3/20/2017 5 10:00 AM 12:00 PM Group1
3/20/2017 5 1:00 PM 2:00 PM Group2
3/20/2017 3 11:00 PM 2:00 PM Group2
3/20/2017 3 1:00 PM 2:00 PM Group2
3/20/2017 4 10:25 AM
I have a listbox for each of the rooms. Here's my pseudocode:
Foreach row in range
If Reservations <>"" AND date = Date 'only want today's events
[write new statement combining intime and outtime values to display in the listbox in the userform]
Which looks like this:
So, I would like to have a userform populated when the workbook is opened to show that room 5 is booked from 10:00 AM to 12:00 AM and 1:00 PM to 2:00 PM, and that room 3 is booked from 11:00 AM to 12:00 AM and 1:00 PM to 2:00 PM.
So, a couple questions:
Would my code end up in Workbook_Open() or UserForm_Initialize()?
How do you combine the two times into one statement?
Thanks

Related

How to get time elapsed till 12:00 AM in same day and rest on next day

I am currently facing difficulty to calculate exact hours between In Time and Out time.
for instance :
Column A
Column B
Column C
Column D
1-1-2022
10:00 PM
1-2-2022
6:00 AM
1-2-2022
10:00 PM
1-3-2022
6:00 AM
Now, my problem is that I have to calculate the time from 10:00 PM till 12:00 AM = 2 Hours and rest to be calculated on the next day adding the previous days hours e.g. we have 6 hours from 12:00 AM till 6:00 AM for 1-2-2022 (Column C) and then 2 hours from 10:00 PM to 12:00 AM.
Looking forward your help.
You must add the date and times together, subtracting the In time from the Out time.
Slightly different method:
B2, D2 and E2 formatted as hh:mm
Then you could use mod() like so:
=MOD(D2-B2,1)

Excel formula: how to sum total time between different time intervals based on a time condition

A B
Departure Date Arrival date
4/9/18 22:40 5/17/18 14:10
8/5/18 18:20 8/22/18 6:00
9/24/18 22:20 10/10/18 6:00
10/22/18 14:00 11/7/18 6:10
12/3/18 22:25 12/19/18 6:00
1/16/19 0:00 1/30/19 0:00
I need some help please. I have to calculate the total travel days for our employees for a specific period of time. I have columns A and B (all are date and time). Column A is the Departure Date, Column B is the Arrival Date.
Question: which is the Total number of travel days between:5/10/2018 and 12/10/2018?
Try this, you may need to edit it to match your needs though...
IF(AND(A4>=$A$1,B4<=$B$1),B4-A4,IF(AND(A4<=A$1,B4<=$B$1),B4-$A$1,IF(AND(A4>=A$1,B4>=$B$1),$B$1-A4)))
See:

Allocating staff to 30 minute intervals

I’m trying to create either a spreadsheet or macro that allows me to put and employee start and finish time in and then show me by each half hour how many half hours they are in.
For example;
Employee 1 - 08:30 to 10:15
The output I would want is;
08:00 0
08:30 1
09:00 1
09:30 1
10:00 0.5
10:30 0
11:00 0
So between 8:30 and 9:00 shows as one because that employee was in during that half hour. But between 10:00 and 10:30 it shows 0.5 because the employee was only in for half of that period
Then as I add more data it tells me which half hours they are covering.
I’ve tried using the ceiling and floor values, counta, countif, countifs, v and hlookups but just can’t get the desired effect.
I’m guessing it would need to be a macro.
Assuming C1 and D1 contain the start and end times of a shift and column A contains the times, use the following formula in column B:
=IF(A2>D$1,C2,IF(AND(A2>=C$1,A3<=D$1),1,MAX(0,A3-D$1)/(1/48)))
Limitations: shifts cannot cross over days, days cannot start before 00:00 or end after 23:59

Excel Formula SUMIFS on date and time using date from given cell

Excel 2016 (office 365, current version)
I have a formula that works, but is manually intensive (i.e. manually edit the date in two places for each formula, and I have to do this for an entire quarter's worth of data):
=SUMIFS('Raw'!$E:$E,'Raw'!$A:$A,">=3/30/2017 0:00",'Raw'!$A:$A,"<=3/30/2017 23:00")
yields: 35.69658611 (Correct, actual sample data below)
I would like to be able to take the date value for the local sheet column M (list of dates for the month) and plug that date directly into the formula so I can copy paste the formula to calculate the rest of the month.
What I have returns a value of zero:
=SUMIFS('Raw'!$E:$E,'Raw'!$A:$A,">="&M3 &" 0:00",'Raw'!$A:$A,"<="&M3 &" 23:00")
DataTable: Raw
Date/Time value: 'Raw' column A
Data to sum: 'Raw' Column E
Both formulas are based on hint from https://www.mrexcel.com/forum/excel-questions/601474-sumifs-date-range-criteria.html
Sample data:
DateTime kW
3/30/17 23:00 0
3/30/17 22:00 0
3/30/17 21:00 0
3/30/17 20:00 0
3/30/17 19:00 0.004455278
3/30/17 18:00 0.5370675
3/30/17 17:00 2.303020833
3/30/17 16:00 4.122186389
3/30/17 15:00 5.415064722
3/30/17 14:00 6.190184167
3/30/17 13:00 3.621349167
3/30/17 12:00 3.292333056
3/30/17 11:00 4.470871944
3/30/17 10:00 2.407315556
3/30/17 9:00 2.269564167
3/30/17 8:00 1.033854722
3/30/17 7:00 0.029317778
3/30/17 6:00 0
3/30/17 5:00 0.000000556
3/30/17 4:00 0
3/30/17 3:00 0
3/30/17 2:00 0
3/30/17 1:00 0.000000278
3/30/17 0:00 0
You must remember how the date format work in Excel.
Every day since 1900/01/00 (arbitrary chosen date) are indexed.
So in short, when you type 2017/03/30 in cell M3 excel really only read "42824".
=SUMIFS('Raw'!$E:$E,'Raw'!$A:$A,">="&$M3 &" 0:00",'Raw'!$A:$A,"<="&$M3 &" 23:00")
Only change required is that you must save date in Text format (i.e, prepend an apostrophe (') to the date, thereby not allowing excel to auto-format as Date).
The final working formula was almost exactly the same as my second one, the only change really was the added $ to lock in the column on the textual concatenation:
=SUMIFS('Raw'!$E:$E,'Raw'!$A:$A,">="&$M3 &" 0:00",'Raw'!$A:$A,"<="&$M3 &" 23:00")
Only change required is that you Must save date in Text format (i.e, prepend an apostrophe (') to the date, thereby not allowing excel to auto-format as Date)
Special thanks to #MrDogme for the trigger that helped me fix the problem.

VLOOKUP not returning for datetime

I have a list of hours when water is used. this list does not generate hours when water was not used. I'll have list like:
Hour Liters
5/3/14 6:00 PM 36.288
5/3/14 7:00 PM 15.328
5/3/14 8:00 PM 1.6
5/4/14 11:00 PM 18.752
5/5/14 12:00 AM 21.664
5/5/14 1:00 AM 21.76
5/5/14 2:00 AM 21.76
Now I want to put them in a serial format so it should be like:
5/3/14 6:00 PM 36.288
5/3/14 7:00 PM 15.328
5/3/14 8:00 PM 1.6
5/3/14 9:00 PM 0.0
5/3/14 10:00 PM 0.0
5/3/14 11:00 PM 0.0
5/4/14 12:00 AM 21.664
5/4/14 1:00 AM 21.76
5/4/14 2:00 AM 21.76
I used VLOOKUP to retrieve for the date values and output the corresponding water used. It shows #N/A. I verified the date decimals using the match function and it gives me true.
Welcome to the world of time, base60 vs. base10 and 15 significant digit floating point precision. I find that sometimes it is simply expedient to give yourself a 'time window' to meet rather than try to round off ranges.
       
The formula in E2 is,
=SUMIFS($B$2:$B$8, $A$2:$A$8, ">"&D2-TIME(0, 0, 2), $A$2:$A$8, "<"&D2+TIME(0, 0, 2))
This give a four (±2) second window to match rather than attempting an exact match on what could be a repeating decimal. That could conceivably be tightened further but is probably fine for your hourly data. Fill down as necessary.
My column of 'serialized' times were generated using =A2 in D2 then =D2+TIME(1, 0, 0) in D3 and filled down.

Resources