getting wrong value when dealing with time in vba excel - excel

I'm trying to do some calculations on my excel sheet where I'm using macros , i'm dealing with time values.
h2 = rg.Cells(i, 5) 'time value
h1 = rg.Cells(i, 4) 'time value
h3 = (TimeValue("23:59") - h1) + (h2 - TimeValue("23:59"))
the problem is for instance when h1="20:00" and h2="07:55" h3 should be 11:55 but it gives 12:05
BTW H1 is entrance time and H2 is time out so h2 is in the next day and i'm trying to calculate the deference of time.
regards

Because H2, as you point out, is a day after H1, you need to factor that in to your calculation.
So to calculate the difference between H1 and H2, 24 hours should be added to H2 first.
h2 + 1dy
07:55 + 24:00 = 31:55
v
-h1
31:55 - 20:00 = 11:55
A single day presented as time is 1 - as time is measured as a decimal fraction of a day.
So among many other ways of doing it, to get the above, your formula should be:
h3 = (h2 + 1) - h1

In fact the correct answer is minus 11:05:00 but Excel doesn't handle negative time. Adding one day to minus 11:05::00 equals positive 12:05:00.

Related

Trying to show a full years amount based on a smaller fraction of the year's total

this is my first post.
Right now, I have a limited set of data ranging from the beginning of this financial year until now. I'm trying to show what a full year's worth of that data would look like.
For example, if the number is at 10, and the data range is from 1/07/2021 - 30/12/2021 (half the year), then the end output should be 20. Or for example, turn a 12 with 3/4 of the year date range to 16 for a full years' worth.
However, my current formula would end up with 15 (10 + "half") rather than (10 + 10)
Right now this is what I have, but I know there's something off on my logic, as the output is smaller than it should be:
D1+((364-(F1-E1))/365)*D1
where E1 is the start date and F1 is the end date, and d1 is the number for that date range
Thanks in advance
endDate - startDate will give you the number of days the data covers.
(endDate - startDate) / 365 will give you what fraction of a year the sample represents.
Let’s say this works out to be 30%, or 0.30.
annualValue * 0.30 = periodValue and therefore we know that periodValue / 0.30 = annualValue.
So there it is, the cell you want the Annual Value in should be:
= periodValue / ( ( endDate - startDate) / 365 )
I will leave it to you to replace each of the three named values in my example to be the correct cell references. I suspect that’s probably:
=D1/((F1-E1)/365) which is the same as (D1*365)/(F1-E1).
The easy way to remember this is that it’s just cross-multiplication.
periodValue / days is proportionate to annualValue / 365. Thus periodValue / days = annualValue / 365. Cross-multiply and you get periodValue * 365 = annualValue * days. Divide both sides by days and you get `annualValue = (periodValue * 365)/days.

Create a rolling date list in Excel

I want to create a rolling list of dates in Excel like so:
Day Date
Day 1 01-Jul-19
Day 1 02-Jul-19
Day 1 03-Jul-19
Day 1 04-Jul-19
Day 1 05-Jul-19
Day 1 06-Jul-19
Day 1 07-Jul-19
Day 2 02-Jul-19
Day 2 03-Jul-19
Day 2 04-Jul-19
Day 2 05-Jul-19
Day 2 06-Jul-19
Day 2 07-Jul-19
Day 2 08-Jul-19
Day 3 03-Jul-19
Day 3 04-Jul-19
Day 3 05-Jul-19
Day 3 06-Jul-19
Day 3 07-Jul-19
Day 3 08-Jul-19
Day 3 09-Jul-19
Day 4 04-Jul-19
. .
. .
. .
So essentially what's happening is that the 7-day range moves forward by one day each time, from a specific start date (in the example above, 01-07-19) until it reaches an end date. Is there an automated way of doing this?
#ashvin10 you can do this in vba, but you can also accomplish this with 2 formulas without using vba at all, here's how:
for illustration purposes we'll just assume you are starting with 07/01/2019 on the first row and your information will be displayed in columns A and B.
in A1 enter the string Day 1
in B1 enter your starting date, like 07/01/2019
in A2 enter this formula: ="Day " & IF(MOD(ROW(A2),7)<>0, MID(A2,5,(LEN(A2)-4)), MID(A2,5,(LEN(A2)-4))+1)
in B2 enter this formula: =IF(A2=A1,B1+1,OFFSET(B2,-7,0)+1)
highlight cells A2 and B2
click on the cross that becomes available on the bottom right hand corner of cell B2
drag down the formula till you hit the end date you desire
the cells are populated with the values you requested in the format you requested
If you absolutely have to have it done using vba please let me know and I can show you how to do it that way as well, but this way is much easier.
EDIT: #ashvin10 I'm so sorry, the original formula I instructed you to put into A2 only works for Day 1 through Day 9, if you go into days past 9 it won't display correctly. I've fixed the formula that should be pasted into A2 so now it will work no matter how many days you go down. I'm so sorry for the confusion.
Alternatively, this can also be done in Python.
import datetime
start_date = '01-07-2019'
end_date = '31-01-2020'
output_file_name = 'rolling dates'
output_file_extension = '.CSV'
delimiter = '\t'
with open((output_file_name + output_file_extension.lower()), 'w+') as file:
header = "Day" + delimiter + "Date" + '\n'
file.write(header)
start_date_object = datetime.datetime.strptime(start_date, '%d-%m-%Y').date()
end_date_object = datetime.datetime.strptime(end_date, '%d-%m-%Y').date()
number_of_days = abs((end_date_object - start_date_object).days)
next_day = start_date_object
for i in range(1, number_of_days + 2):
for j in range(7):
file.write(("Day {0}" + delimiter + next_day.strftime('%d-%m-%Y') + '\n').format(i))
next_day += datetime.timedelta(days=1)
start_date_object += datetime.timedelta(days=1)
next_day = start_date_object
After running the code above, I simply created a blank Excel file and then imported the data from the CSV file output by this code.
This is arguably more complicated than #gharbad-the-weak's answer but thought I'd include this anyway.

Excel TIME duration over 24 hours

Data is:
A1 = 29. B1 = 30. C1 = 2.
D1 = TIME(A1, B1, C1).
How can I get D1 to return 29:30:02?
Formatting the D1 cell to [hh]:mm:ss does NOT work!
If I ran for 29 hours in a month, Excel thinks I ran for only 5 hours.
Thank you.
Edit: Thank you!! I got it!!! D1 = TIME(A1, B1, C1) + INT(A1 / 24)
The result you get is exactly how TIME function works:
Hour Required. A number from 0 (zero) to 32767 representing the
hour. Any value greater than 23 will be divided by 24 and the
remainder will be treated as the hour value. For example, TIME(27,0,0)
= TIME(3,0,0) = .125 or 3:00 AM.
You could do
=A1/24+B1/24/60+C1/24/60/60 in D1
Then the resulting value formatted as [hh]:mm:ss will show 29:30:02.
That is because 1 is 1 day in Excel. So 1/24 is 1 hour, 1/24/60 is 1 minute and 1/24/60/60 is 1 second.
Time is trying to give you clock time so 29hrs=5am.
So if you want that result how about just:
D1=CONCATENATE(A1,":",B1,":",C1)
If then you want the 02 you would have to interject a little formatting to those columns...
Very similar solution as given above by Axel Richter is shown in support article Add or subtract time by Microsoft.
Basically it boils down to using custom format of [h]:mm;#. However I am not sure, what the exact difference to [hh]:mm:ss (or more precisely to [hh]:mm) is - if at all? A quick check in Excel did not reveal any differences to me.
However, as the support article covers topic of adding and subtracting of time values pretty comprehensively, I hope it will be useful to be cited here.

How to count hours in excel

I have xls file in following format
Name 1 2 3 4
John 09:00-21:00 09:00-21:00
Amy 21:00-09:00 09:00-21:00
Where 1,2,3,4 and so on represent days of current month,
09:00-21:00 - working hours.
I want to calculate salary based on the following conditions:
09:00-21:00 - 10$/hour
21:00-00:00 - 15$/hour
00:00-03:00 - 20$/hour
etc.
and so on (every hour can have it's own cost, for example 03:00-04:00 - 20$/hour, 04:00-05:00 - 19$/hour, etc.)
How can i accomplish this using only Excel (functions or VBA)?
P.S. Easy way: export to csv and process in python/php/etc.
Here is a non-VBA solution. It's a pretty nasty formula, but it works. I am sure it could be made even easier to use and understand with some more ingenuity:
Assuming the spreadsheet is set up like this:
Enter this formula in cell G1 and drag down for your data set:
=IF(ISBLANK(B2),"",IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2),SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B2,2),$Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2)),SUMIF($Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B2,2),$P$2:$P$24)))
To explain the formula in detail:
IF(ISBLANK(B2),"" will return a empty string if there is no time for a given person / day combination.
LEFT(B2,2) extracts the start-time into an hour.
Mid(B2,Find("-",B2)+1,2) extracts the end-time into an hour.
IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2) will check if the start-time is less than the end-time (meaning no over-night work). If the start-time is less than the end-time, it will use this formula to calculate the total cost per hour: SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B3,2),$Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2))
If the start-time is higher than the end-time (meaning overnight work), it will use this formula to calculate: SUMIF($Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B3,2),$P$2:$P$24)
The use of the Find("-",[cell]) splits the start-and- end times into values excel can use to do math against the Time / Cost table.
The formula in column Q of the Time / Cost table is =VALUE(MID(O2,FIND("-",O2)+1,2)) and turns the ending hour to consider the cost into a value Excel can use to add, instead of having the text from your original source format.
Do this in VBA! It is native to excel and is easy to learn. Functionally, I would loop through the table, write a function to calculate the dollars earned based on the info given. If you want your results to be live updating (like a formula in excel) you can write a user defined function. A helpful function might be an HoursIntersect function, as below:
Public Function HoursIntersect(Period1Start As Date, Period1End As Date, _
Period2Start As Date, Period2End As Date) _
As Double
Dim result As Double
' Check if the ends are greater than the starts. If they are, assume we are rolling over to
' a new day
If Period1End < Period1Start Then Period1End = Period1End + 1
If Period2End < Period2Start Then Period2End = Period2End + 1
With WorksheetFunction
result = .Min(Period1End, Period2End) - .Max(Period1Start, Period2Start)
HoursIntersect = .Max(result, 0) * 24
End With
End Function
Then you can determine the start and end time by splitting the value on the "-" character. Then multiply each payment schedule by the hours worked within that time:
DollarsEarned = DollarsEarned + 20 * HoursIntersect(StartTime, EndTime, #00:00:00#, #03:00:00#)
DollarsEarned = DollarsEarned + 10 * HoursIntersect(StartTime, EndTime, #09:00:00#, #21:00:00#)
DollarsEarned = DollarsEarned + 15 * HoursIntersect(StartTime, EndTime, #21:00:00#, #00:00:00#)
I have a method that uses nothing but formulas. First create a lookup table which contains every hour and rate in say columns K & L, something like this:
K L
08:00 15
09:00 10
10:00 10
11:00 10
12:00 10
13:00 10
14:00 10
15:00 10
16:00 10
17:00 10
18:00 10
19:00 10
20:00 10
21:00 15
22:00 15
23:00 15
Make sure you enter the hours as text by entering a single quote before the digits.
Then if your hours were in cell B2 you could then use this formula to calculate the total:
=SUM(INDIRECT("L"&MATCH(LEFT(B2,5),K2:K40,0)&":L"&MATCH(RIGHT(B2,5),K2:K40,0)))
All the formula is doing is getting the left and right text of your work time, using MATCH to find their positions in the lookup table which is used to create a range address which is then passed to SUM via the INDIRECT function.
If you need to worry about minutes all you need to do is create a bigger lookup table which holds every minute of the day. You may need to add some extra logic if your work days span midnight.

Derive an End date by Man hours and a start date in Excel-2010

I need a formula which calculates the End Date (with Time) when total Man Hours and Start Date are given. [Excel 2010]
Criteria:
1 Man Day = 8 Hours (1 hour is break time)
Work Start Time = 10:00
Work End Time = 19:00
1 Man Week = Mon - Fri
Holidays, if any, should not be counted
For example:
Cell E7 = Man Hours = 10 Hrs
Cell F7 = Start Date = 26-Jun-15 13:00
Cell G7 = End Date = ???? (Ideally 29-Jun-15 14:00)
*27th and 28th are weekends
Thank you!
margin of error for the estimate of duration from estimate of effort is too big for an hourly granularity to make any sense as long as real people and realistic plans are concerned
however, if we have an estimate of duration in working hours on the input (as opposed to calendar days/weeks/months which are more common units of duration estimates) and we need to calculate the end date-time, we could:
compute helper column with the would-be end hour if there were no closing hours (e.g. in cell H7, in Excel Date units = fractions of a day)
=MOD(F7, 1) + MOD(E7, 8)/24
check if the would-be hour is before the closing hours and adjust the end day + end time accordingly. If it's after, move to the next workday and adjust time, e.g. for simplicity assume all breaks are at the beginning of a day, so we can subtract the number of hours in a workday:
=IF(H7 <= 19/24,
WORKDAY(F7, E7/8, holidays) + H7,
WORKDAY(F7, E7/8 + 1, holidays) + H7 - 8/24)
assume you saved your national holidays on column I, you can try below in cell H7:
=IF(HOUR(F7+E7/24)>19,((F7+E7/24)-INT(F7+E7/24)-0.792)+(WORKDAY(F7,1,I:I))+0.417,F7+E7/24)

Resources