Solved

# Automatically Moving Time Punch Data

Posted on 2013-10-24
Medium Priority
202 Views
I have a report that is exported from a time-clock program and I need to re-align the punch times.  The attached workbook has the data table with what are 3 typical variations of punches I see.

Lines 4 and 5:  These are standard daily punches, the punches occur on the same date the first line is the morning, the second line is after lunch.  On this one I need to move the second set of punches (in red text) to the end of the previous line (dark blue boxes)

Line 17: This punch is when someone forgets to punch out for lunch/break or works less than 5 hours.  For this punch I need to move that end of day punch (green text) to the end of that line (green box)

Lines 21 and 22:  This set of punches are for our graveyard shift and the first punch in is usually between 10:00pm and 11:59pm on one day (noted in black bold, 10/19/2013) and the remaining punches for that shift are actually the next day (noted in blue bold, 10/20/2013).  I need to move the second set of punches (brown text) to the end of the previous line (dark purple boxes)

Thank you,
Edwin
Punch-Extract-Report-2013-10-24.xls
0
Question by:gixxer1020
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 5

LVL 81

Expert Comment

ID: 39599182
You didn't say whether you wanted a formula or a macro.

Here is a formula for cell H6 that may be copied down:
=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),F6=H5),F6=""),"",
IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),F7,""))

Here is a formula for cell I6 that may be copied down:
=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),G6=I5),G6=""),"",
IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),G7,IF(G6-F6>5/24,G6,"")))

The equivalent macro is:
``````Sub PunchCardReporter()
Dim rg As Range
With Worksheets("Sheet1")
Set rg = .Range("E6")    'First Date
Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)).EntireRow    'All the data
End With
rg.Cells(1, "H").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),F6=H5),F6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),F7,""""))"
rg.Cells(1, "I").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),G6=I5),G6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),G7,IF(G6-F6>5/24,G6,"""")))"
rg.Columns("H").FillDown
rg.Columns("I").FillDown
rg.Columns("H").Formula = rg.Columns("H").Value
rg.Columns("I").Formula = rg.Columns("I").Value
End Sub
``````

If you want to delete the rows where you moved data, I suggest AutoFilter on column I looking for empty strings (="") which look like blanks. Those rows can be deleted.
Punch-Extract-Report-2013-10-24Q.xls
0

Author Comment

ID: 39599435
Thanks byundt,

Works great...can you move the data instead of copying it?

Thanks,
Edwin
0

Author Comment

ID: 39599439
Oh, and I'll be using the Macro

Thanks,
Edwin
0

LVL 81

Expert Comment

ID: 39599518
Edwin,

Could you please post a file with a worksheet showing what the results ought to look like after the macro has run on your sample data? I ask because I'm not understanding why you would want to move the contents of F7:G7 into H6:I6 while leaving the rest of row 7 alone.

0

Author Comment

ID: 39599542

File attached.
Table below the original is the end result I'm going for...the sorting and elimination of extra rows i can do, its the code you wrote I had no idea of how to do

Thanks,
Edwin
Punch-Extract-Report-2013-10-24-.xls
0

LVL 81

Expert Comment

ID: 39599566
Edwin,
Here is the revised macro. I modified the formula for cell I6 to allow for the possibility that a person might work until the following day, but not do the punch out/in for their lunch break. This affects line 20. I also added code to delete the rows where data had been copied from.
``````Sub PunchCardReporter()
Dim rg As Range
Dim i As Long
Application.ScreenUpdating = False
With Worksheets("Sheet1")
Set rg = .Range("E6")    'First Date
Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)).EntireRow    'All the data
End With
rg.Cells(1, "H").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),F6=H5),F6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),F7,""""))"
rg.Cells(1, "I").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),G6=I5),G6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),G7,IF(MOD(G6-F6,1)>5/24,G6,"""")))"
rg.Columns("H").FillDown
rg.Columns("I").FillDown
rg.Columns("H").Formula = rg.Columns("H").Value
rg.Columns("I").Formula = rg.Columns("I").Value
For i = rg.Rows.Count To 1 Step -1
If rg.Cells(i, "I").Value = "" Then rg.Rows(i).EntireRow.Delete
Next
End Sub
``````
Punch-Extract-Report-2013-10-24Q.xls
0

LVL 81

Expert Comment

ID: 39599595
One more revision needed to the macro to handle the possibility that a person worked graveyard shift and went home before working 5 hours. Such a row should not be deleted even though columns H and I are blank.
``````Sub PunchCardReporter()
Dim rg As Range
Dim i As Long
Application.ScreenUpdating = False
With Worksheets("Sheet2")
Set rg = .Range("E6")    'First Date
Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)).EntireRow    'All the data
End With
rg.Cells(1, "H").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),F6=H5),F6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),F7,""""))"
rg.Cells(1, "I").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),G6=I5),G6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),G7,IF(MOD(G6-F6,1)>5/24,G6,"""")))"
rg.Columns("H").FillDown
rg.Columns("I").FillDown
rg.Columns("H").Formula = rg.Columns("H").Value
rg.Columns("I").Formula = rg.Columns("I").Value
For i = rg.Rows.Count To 1 Step -1
If (rg.Cells(i, "I").Value = "") And _
(rg.Cells(i, "F").Value = rg.Cells(i - 1, "H").Value) And (rg.Cells(i, "G").Value = rg.Cells(i - 1, "I").Value) Then _
rg.Rows(i).EntireRow.Delete
Next
End Sub
``````
0

Author Comment

ID: 39599758

On lines 17 and 21 the punches that you moved to the end were not deleted out of their original location.  Other than that the code is amazing

Thank you,
Edwin
0

LVL 81

Accepted Solution

byundt earned 2000 total points
ID: 39603439
Edwin,
I added an ElseIf section to handle the case of column G value being moved to column I.
``````Sub PunchCardReporter()
Dim rg As Range
Dim i As Long
Application.ScreenUpdating = False
With Worksheets("Sheet1")
Set rg = .Range("E6")    'First Date
Set rg = Range(rg, .Cells(.Rows.Count, rg.Column).End(xlUp)).EntireRow    'All the data
End With
rg.Cells(1, "H").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),F6=H5),F6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),F7,""""))"
rg.Cells(1, "I").Formula = "=IF(OR(AND(\$C5=\$C6,OR(\$E5=\$E6,\$E5=\$E6-1),G6=I5),G6=""""),""""," & _
"IF(AND(\$C6=\$C7,OR(AND(\$E6=\$E7,\$G7>\$F7),\$G6<\$F6)),G7,IF(MOD(G6-F6,1)>5/24,G6,"""")))"
rg.Columns("H").FillDown
rg.Columns("I").FillDown
rg.Columns("H").Formula = rg.Columns("H").Value
rg.Columns("I").Formula = rg.Columns("I").Value
For i = rg.Rows.Count To 1 Step -1
If (rg.Cells(i, "I").Value = "") And _
(rg.Cells(i, "F").Value = rg.Cells(i - 1, "H").Value) And (rg.Cells(i, "G").Value = rg.Cells(i - 1, "I").Value) Then
rg.Rows(i).EntireRow.Delete
ElseIf rg.Cells(i, "G").Value = rg.Cells(i, "I").Value Then
rg.Cells(i, "G").ClearContents
End If
Next
End Sub
``````
Punch-Extract-Report-2013-10-24Q.xls
0

Author Closing Comment

ID: 39615905

Edwin
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calculâ€¦
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
###### Suggested Courses
Course of the Month10 days, 8 hours left to enroll