View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
RyanH RyanH is offline
external usenet poster
 
Posts: 586
Default Macro to calculate hours per work order

This code is untested, but I think it will get you started. I am assuming
you want all of Mondays Hours summed in Destination Wks Col. C, Tuesday Col.
D, and so on. If you get any errors let me know and I will try to help.
If this helps you, please let me know by clicking "YES" below.

Option Explicit

Sub SumHours()

Dim lngLastWorkOrder As Long
Dim rngWorkOrders As Range
Dim lngLastRow As Long
Dim rngSource As Range
Dim cell As Range
Dim rngFoundOrder As Range

With Sheets("Dest_Wksht")
' find last row in workorder col
lngLastWorkOrder = .Cells(Rows.Count, "B").End(xlUp).Row
If lngLastWorkOrder < 6 Then
MsgBox "There are no workorders to process.", vbInformation
Exit Sub
End If

' set range of work orders
Set rngWorkOrders = .Range("B6:B" & lngLastWorkOrder)
End With

With Sheets("Source_Wksht")
' find last row in source wks
lngLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
If lngLastWorkOrder < 2 Then
MsgBox "There are no workorders to process.", vbInformation
Exit Sub
End If

'set range to scan
Set rngSource = .Range("A2:A" & lngLastRow)
End With

' find workorders in source wks
For Each cell In rngWorkOrders
Set rngFoundOrder = rngSource.Find _
(What:=cell.Value, _
LookIn:=xlValues, _
LookAt:=xlWhole)

' if workorder if found add the hours to the destination wks
If Not rngFoundOrder Is Nothing Then
Select Case Format(rngFoundOrder.Offset(0, 1).Value, "dddd")

Case "Monday"
With Sheets("Dest_Wksht").Cells(cell.Row, "C")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Tuesday"
With Sheets("Dest_Wksht").Cells(cell.Row, "D")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Wednesday"
With Sheets("Dest_Wksht").Cells(cell.Row, "E")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Thursday"
With Sheets("Dest_Wksht").Cells(cell.Row, "F")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Friday"
With Sheets("Dest_Wksht").Cells(cell.Row, "G")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Saturday"
With Sheets("Dest_Wksht").Cells(cell.Row, "H")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With
Case "Sunday"
With Sheets("Dest_Wksht").Cells(cell.Row, "I")
.Value = .Value +
Sheets("Source_Wksht").Cells(rngFoundOrder.Row, "D").Value
End With

End Select
End If
Next cell

End Sub

--
Cheers,
Ryan


"Jay" wrote:

Im clear on what I want to do, but I dont know how to proceed.
I need a macro that will:

1. Read WorkOrderNo from a list of work orders starting in cell B6 in
Dest_Wksht
2. Find all instances of WorkOrderNo in Source_Wksht that fall on Date1
(Monday), and populate cell C6 in Dest_Wksht with the total hours worked for
that Work Order number on that day.
3. Repeat for Date2 (Tuesday) thru Date7 (Sunday)
4. Repeat with cel B7 from Dest_Wksht thru until end of list

Where do I begin?
Thanks for any help.

Source_Wksht looks like this:
A B C D
2 Work Order Time In Time Out Time elapsed
3 1221 10/21/2008 10:03 10/21/2008 12:16 2:13
4 1221 10/21/2008 12:36 10/21/2008 15:58 3:21
5
6 1223 10/20/2008 14:21 10/20/2008 16:27 2:06
7 1223 10/21/2008 8:00 10/21/2008 10:03 2:03