Macro

Excelexcel86

Board Regular
Joined
Feb 28, 2023
Messages
99
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi guys I’ve had help with a formula to calculate certain times but on the advice of another member he recommends that I might need a macro so the issue is I have 4 cells which I will show example below I need a macro assigned to the one cell that is labelled time inside warehouse that calculates from when the item came in the warehouse to when it left the warehouse now the work hours are 06:00 to 14.30 Monday to Thursday and 6:00 to 11:00 on a Friday it also needs to calculate the hours if it goes into another day that is in the start time

Time entered warehouse time exited warehouse time inside warehouse for work hours Total time
01/03/2023 07:3001/03/2023 13.30Should be 6 hours as it’s inside work hours
01/03/2023 09.0002/03/2023 08:00Should be be 7:30 hours as it has gone over to the next day and into start time again Should be 23 hours
10/03/2023 08:0010/03/2023 12:00This is a Friday so should be 3hrs as finish at 11:00Should be 4 hours
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,
Which formula are you currently using ?

Your macro will very probably follow the very same logic ...
 
Upvote 0
=24IF(INT(B2)=INT(A2), MEDIAN(MOD(B2,1),TIME(6,0,0),TIME(14,30,0)) -MEDIAN(MOD(A2,1),TIME(6,0,0),TIME(14,30,0)), IF(INT(B2)-INT(A2)>=1, MEDIAN(MOD(B2,1),TIME(6,0,0),TIME(14,30,0))-TIME(6,0,0) +TIME(14,30,0)-MEDIAN(MOD(A2,1),TIME(6,0,0),TIME(14,30,0)) +(( INT(B2)-INT(A2)-1)*(TIME(14,30,0)-TIME(6,0,0))),""))



This is the formula but obviously it does take the Friday into account
 
Upvote 0
My suggestion is that you go with a User Defined Function.
For example:
-insert somewhere in your file a table with the working hours for each of the weekdays (see the image, the orange area)
-copy the folllowing code into a standard module of your vba project:
VBA Code:
Function WorkedH(ByRef InOut As Range, ByRef OfficeH As Range) As Date
Dim whArr, I As Double
Dim MinTot As Long, cWDay As Long
'
whArr = OfficeH.Value
For I = CDbl(InOut.Cells(1, 1)) To Int(InOut.Cells(1, 2)) + 20000 / 1440 Step 1 / 1440
    cWDay = Weekday(Int(I), 2)
    If (I - Int(I)) >= whArr(cWDay, 2) And (I - Int(I)) <= whArr(cWDay, 3) Then
        MinTot = MinTot + 1
    End If
    If I > CDbl(InOut.Cells(1, 2)) Then Exit For
Next I
WorkedH = Round(MinTot / 5, 0) / 1440 * 5
End Function
Now you have available the Function WorkingH with the following syntax:
Excel Formula:
WorkingH(Start/EndDate&Time, DailyWorkingHours) as Date

In my test sheet (see the image) I used in I2:
Excel Formula:
=WorkedH(A2:B2,$L$1:$N$7)
Copy the formula down as necessary
 

Attachments

  • WH_Immagine 2023-03-08 111950.jpg
    WH_Immagine 2023-03-08 111950.jpg
    91.5 KB · Views: 16
Upvote 0
My suggestion is that you go with a User Defined Function.
For example:
-insert somewhere in your file a table with the working hours for each of the weekdays (see the image, the orange area)
-copy the folllowing code into a standard module of your vba project:
VBA Code:
Function WorkedH(ByRef InOut As Range, ByRef OfficeH As Range) As Date
Dim whArr, I As Double
Dim MinTot As Long, cWDay As Long
'
whArr = OfficeH.Value
For I = CDbl(InOut.Cells(1, 1)) To Int(InOut.Cells(1, 2)) + 20000 / 1440 Step 1 / 1440
    cWDay = Weekday(Int(I), 2)
    If (I - Int(I)) >= whArr(cWDay, 2) And (I - Int(I)) <= whArr(cWDay, 3) Then
        MinTot = MinTot + 1
    End If
    If I > CDbl(InOut.Cells(1, 2)) Then Exit For
Next I
WorkedH = Round(MinTot / 5, 0) / 1440 * 5
End Function
Now you have available the Function WorkingH with the following syntax:
Excel Formula:
WorkingH(Start/EndDate&Time, DailyWorkingHours) as Date

In my test sheet (see the image) I used in I2:
Excel Formula:
=WorkedH(A2:B2,$L$1:$N$7)
Copy the formula down as necessary
Hi thanks for that how do I link it to my other sheet that you seen in my other question to pick up the work hours
 
Upvote 0
My code require somewhere a table with the weekly working hours, like the one in orange in my previous image. You have something similar in the area outlined in orage in the attached image; but it needs the first column to be named 1 /2 /3 etc rather than Mon /Tue /Wed etc, and should also include lines 6=Sat and 7=Sun
The area in green is the Start/EndDate&time, first parametre for WorkedH
 

Attachments

  • WH_Immagine 2023-03-08 114635.jpg
    WH_Immagine 2023-03-08 114635.jpg
    146.6 KB · Views: 13
Upvote 0
My code require somewhere a table with the weekly working hours, like the one in orange in my previous image. You have something similar in the area outlined in orage in the attached image; but it needs the first column to be named 1 /2 /3 etc rather than Mon /Tue /Wed etc, and should also include lines 6=Sat and 7=Sun
The area in green is the Start/EndDate&time, first parametre for WorkedH
Does it have to be in the same sheet tho or can I keep it in separate sheet as it is and link it to where my data is ?
 
Upvote 0
It may be in a separate sheet, as with any excel function; for example
Excel Formula:
=WorkedH(A2:B2,Sheet123!$A$1:$C$7)
In this example the weekly table is in Sheet123 range A1:C7
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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