MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2002, 05:59 AM   #1
sweethrtcc
New Member
 
Join Date: Apr 2002
Posts: 8
Default

I need help converting a date and time to total hours on a job but if it spans past the normal work hours to only add actual work hours (not evening/weekend hours)
EX:
04/02/2002 09:41 AM
04/04/2002 10:42 AM
If normal work hours are 6:00 AM - 2:00 PM the total time should be 17 hr 1 min
sweethrtcc is offline   Reply With Quote
Old Apr 8th, 2002, 06:46 AM   #2
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi

Try:

=NETWORKDAYS($A$1,$A$2)*(8/24) and Custom format as "[h]:mm:ss"

This assumes the normal working hours are 8, eg 6am-2pm
Dave Hawley is offline   Reply With Quote
Old Apr 9th, 2002, 08:28 AM   #3
sweethrtcc
New Member
 
Join Date: Apr 2002
Posts: 8
Default

I tried this but it only gives me the total days (in hours ie 16:00) instead of the time elapsed including the minutes
sweethrtcc is offline   Reply With Quote
Old Apr 9th, 2002, 09:42 AM   #4
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Try:

="14:00"-TEXT(A1,"h:mm")+((NETWORKDAYS(A1,A2)-2)*"8:00")+(TEXT(A2,"h:mm")-"6:00")

format as [h]:mm

I haven't tested it over a weekend or holiday, just with what you gave, let me know if you need any more.

__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Apr 9th, 2002, 10:17 AM   #5
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
I decided to write my first user defined function. I think it actually works.
Does not do holidays yet. Let me know if it works and I'll figure out how to add holidays as well.

Copy this code into a standard module or in your personal macro book.

Call it like a normal function.

=nhw(BeginningDateTime,EndingDateTime)

if the beginning date and time is in cell A1
ending date and time is in B1

then place:
= nhw(A1,B1)
in another cell
Will output "17 hrs. - 1 min."
Tested over a weekend as well.

Will not allow an invalid beginning or ending time to be entered. After 2 PM, before 6 am, or on a weekend day.

Let me know if it works.

Function nhw(StartingTime As Date, EndingTime As Date)
Dim TotalNetHoursWorked
Dim ThisDayEnd As Date
Dim ThisDayBegin As Date
Dim Cntr As Integer

If StartingTime = 0 Then Exit Function
If EndingTime = 0 Then Exit Function
If Format(StartingTime, "DDDD") = "Saturday" Or _
Format(StartingTime, "DDDD") = "Sunday" Then
MsgBox "Starting Date on " & Format(StartingTime, "DDDD") & " is Invalid"
nhw = "Invalid Starting Date"
Exit Function
End If
If Format(EndingTime, "DDDD") = "Saturday" Or _
Format(EndingTime, "DDDD") = "Sunday" Then
MsgBox "Ending Date on " & Format(EndingTime, "DDDD") & " is Invalid"
nhw = "Invalid Ending Date"
Exit Function
End If
If Hour(StartingTime) < 6 Or Hour(StartingTime) > 14 Then
MsgBox "Starting Time is invalid. Must be between 06:00 AM to 02:00 PM"
nhw = "Invalid Starting Time"
Exit Function
End If
If Hour(EndingTime) < 6 Or Hour(EndingTime) > 14 Then
MsgBox "Ending Time is invalid. Must be between 06:00 AM to 02:00 PM"
nhw = "Invalid Ending Time"
Exit Function
End If
If DateDiff("d", StartingTime, EndingTime) > 365 Then
MsgBox "Out of range - Greater than one year"
nhw = "Invalid Ending Time"
Exit Function
End If
ThisDayEnd = Month(StartingTime) & "/" & Day(StartingTime) & "/" & _
Year(StartingTime) & " " & "14:00:00 PM"
If ThisDayEnd > EndingTime Then
TotalNetHoursWorked = DateDiff("n", StartingTime, EndingTime)
GoTo AllDone
End If

TotalNetHoursWorked = DateDiff("n", StartingTime, ThisDayEnd)
For Cntr = 1 To 365
ThisDayBegin = Month(StartingTime + Cntr) & "/" & Day(StartingTime + Cntr) & "/" & _
Year(StartingTime + Cntr) & " " & "06:00:00 AM"
ThisDayEnd = ThisDayBegin + #8:00:00 AM#
If ThisDayBegin > EndingTime Then Exit For
If ThisDayEnd > EndingTime Then
If Format(ThisDayEnd, "MMMM") = "Saturday" Then Exit For
TotalNetHoursWorked = TotalNetHoursWorked + DateDiff("n", ThisDayBegin, EndingTime)
Exit For
End If
If Format(ThisDayEnd, "DDDD") <> "Saturday" And _
Format(ThisDayEnd, "DDDD") <> "Sunday" Then _
TotalNetHoursWorked = TotalNetHoursWorked + DateDiff("n", ThisDayBegin, ThisDayEnd)
Next
AllDone:
nhw = Int(TotalNetHoursWorked / 60) & " hrs. - " & TotalNetHoursWorked Mod 60 & " min."
End Function

Tom


[ This Message was edited by: TsTom on 2002-04-09 09:27 ]
Tom Schreiner is offline   Reply With Quote
Old Apr 10th, 2002, 10:40 AM   #6
sweethrtcc
New Member
 
Join Date: Apr 2002
Posts: 8
Default

Thanks for the answers. They both worked great. I do, however, love the one with the weekends. If it would not be too much trouble I would love to have the holidays also figured. You mentioned that you could probably do so. If not, then thanks for all your help. It was greatly appreciated.
sweethrtcc is offline   Reply With Quote
Old Apr 10th, 2002, 11:08 AM   #7
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Several of us offered to include holidays.
Which function did you use and what are your companies holidays?
Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 10th, 2002, 11:37 AM   #8
sweethrtcc
New Member
 
Join Date: Apr 2002
Posts: 8
Default

TsTom,
I used yours. Holidays are Jan 1, Memorial day (this year May 27), July 4, Labor Day (this year Sep 2), Thanksgiving & day after (Nov 28, 29), Christmas (Dec 24-31).

Again, Thanks so much.
sweethrtcc is offline   Reply With Quote
Old Apr 11th, 2002, 02:09 AM   #9
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi sweethrtcc.
I have 'till memorial day to get his figured out, right?
Any one want to risk a migraine and make my function work with holidays???
Dave Hawley! Do not make fun of Tom's first function! Remember This!
I can do in 100 lines, what you can do in two! Am tired. Later...

Will Post it here on May 6
Search for your username sweethrtcc
Or you can E-Mail me
TsTom@hotmail.com
Tom
Tom Schreiner is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 05:19 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes