Calculating Working Hours Without Weekends, Having 15hrs Per Day? Complex Formula Understanding

%3CLINGO-SUB%20id%3D%22lingo-sub-2184549%22%20slang%3D%22en-US%22%3ECalculating%20Working%20Hours%20Without%20Weekends%2C%20Having%2015hrs%20Per%20Day%3F%20Complex%20Formula%20Understanding%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2184549%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3EI'm%20in%20pursuit%20of%20creating%20a%20VBA%20macro%20that%20automates%20and%20simplifies%20a%20task%20of%20sorting%20of%20data%20that%20contains%20anywhere%20from%20thousands%20to%20tens%20of%20thousands%20of%20rows.%20This%20sorting%20macro%20also%20creates%20an%20additional%20column%20of%20calculation%20that%20calculates%20the%20number%20of%20hours%20worked%20per%20day%20and%20exclude%20non-working%20hours%20in%20the%20calculation.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20stumbled%20upon%20what%20I%20believe%20is%20a%20complex%20formula%20that%20I%20modified%20as%20per%20my%20needs%20but%20what%20I%20can't%20figure%20out%20for%20the%20life%20of%20me%20is%20what%20the%20%22IF%22%20function%20is%20doing%20seeing%20how%20no%20logical%20operator%20is%20used.%20Because%20of%20this%2C%20I'm%20unable%20to%20convert%20this%20formula%20into%20a%20macro.%20I%20tried%20using%20macro%20recorder%20and%20it%20just%20uses%20Active.Cell.FormulaR1C1%20to%20paste%20the%20formula%20as%20is%2C%20whereas%20when%20I%20try%20to%20do%20the%20same%2C%20it%20gives%20an%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyways%2C%20the%20formula%20in%20question%20is%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D(NETWORKDAYS.INTL(StartDate%2CEndDate)-1)*(EndTime-StartTime)%2BIF(NETWORKDAYS(EndDate%2CEndDate)%2CMEDIAN(MOD(EndDate%2C1)%2CEndTime%2CStartTime)%2CEndTime)-MEDIAN(NETWORKDAYS(StartDate%2CStartDate)*MOD(StartDate%2C1)%2CEndTime%2CStartTime)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhere%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EStartDate%3C%2FSTRONG%3E%20%3D%20Start%20Date%20of%20a%20task%20in%20%22dd%2Fmm%2Fyyyy%20hh%3Amm%3Ass%22%20format%3C%2FP%3E%3CP%3E%3CSTRONG%3EEndDate%3C%2FSTRONG%3E%20%3D%20End%20Date%20of%20a%20task%20in%20%22dd%2Fmm%2Fyyyy%20hh%3Amm%3Ass%22%20format%3C%2FP%3E%3CP%3E%3CSTRONG%3EStartTime%3C%2FSTRONG%3E%20%3D%20Start%20of%20working%20hours%20i.e.%20%2208%3A00%20AM%22%20format%3C%2FP%3E%3CP%3E%3CSTRONG%3EEndTime%3C%2FSTRONG%3E%20%3D%20End%20of%20working%20hours%20i.e.%20%2211%3A00%20PM%22%20format.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20you%20can%20see%2C%20the%20%22IF%22%20contains%20NetworkDays.Intl%26nbsp%3B%3CSTRONG%3E%3CEM%3Ewithout%3C%2FEM%3E%3C%2FSTRONG%3E%20a%20logical%20operator%20and%20then%20just%20proceeds%20to%20returning%20a%20value%20if%20true%20or%20false.%20This%20is%20what%20is%20confusing%20me%20as%20to%20how%20it%20is%20working%2C%20as%20it%20works%20OK%20when%20using%20it%20as%20a%20formula%20in%20cell%20directly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooking%20forward%20to%20any%20explanations%20that%20I%20might%20be%20missing.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20You!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2184549%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2184571%22%20slang%3D%22en-US%22%3ERe%3A%20Calculating%20Working%20Hours%20Without%20Weekends%2C%20Having%2015hrs%20Per%20Day%3F%20Complex%20Formula%20Understanding%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2184571%22%20slang%3D%22en-US%22%3EI%20guess%20this%20if%20evaluates%20the%20networkdays%20return%20value%2C%20if%20it%20is%200%20it%20will%20return%20false%20if%20it%20returns%20another%20value%20it%20will%20return%20true.%3C%2FLINGO-BODY%3E
Occasional Visitor

Hello All,

I'm in pursuit of creating a VBA macro that automates and simplifies a task of sorting of data that contains anywhere from thousands to tens of thousands of rows. This sorting macro also creates an additional column of calculation that calculates the number of hours worked per day and exclude non-working hours in the calculation.

 

I stumbled upon what I believe is a complex formula that I modified as per my needs but what I can't figure out for the life of me is what the "IF" function is doing seeing how no logical operator is used. Because of this, I'm unable to convert this formula into a macro. I tried using macro recorder and it just uses Active.Cell.FormulaR1C1 to paste the formula as is, whereas when I try to do the same, it gives an error.

 

Anyways, the formula in question is:

 

=(NETWORKDAYS.INTL(StartDate,EndDate,"0000000")-1)*(EndTime-StartTime)+IF(NETWORKDAYS.INTL(EndDate,EndDate,"0000000"),MEDIAN(MOD(EndDate,1),EndTime,StartTime),EndTime)-MEDIAN(NETWORKDAYS.INTL(StartDate,StartDate,"0000000")*MOD(StartDate,1),EndTime,StartTime)

 

 

Where:

StartDate = Start Date of a task in "dd/mm/yyyy hh:mm:ss" format

EndDate = End Date of a task in "dd/mm/yyyy hh:mm:ss" format

StartTime = Start of working hours i.e. "08:00 AM" format

EndTime = End of working hours i.e. "11:00 PM" format.

 

As you can see, the "IF" contains NetworkDays.Intl without a logical operator and then just proceeds to returning a value if true or false. This is what is confusing me as to how it is working, as it works OK when using it as a formula in cell directly.

 

Looking forward to any explanations that I might be missing.

 

Thank You!

1 Reply
I guess this if evaluates the networkdays return value, if it is 0 it will return false if it returns another value it will return true.