VBA - consolidating list

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I have a report, its a list of employee punches that I want to consolidate. Theres a new row for each punch, which I dont want.

Book1
ABCDEFGHIJKLM
1BALANCE DATEFIRST NAMELAST NAMEEMPLOYEE IDSHIFTFCLM AREAManager IDPUNCH DATEPUNCH TIMESITEIDUSERNAMESERVERSOURCE SYSTEM
203/12/2021102989383DH5C07003 -10920924103/12/202106:51:00 AM
303/12/2021109064517DC4C07003 -10692818703/12/202107:03:00 AM
403/12/2021105016027DB0CACOM3 -10920924103/12/202107:08:00 AM
503/12/2021109399457DB3C07003 -10920924103/12/202107:00:00 AM
603/12/2021109154451DB3C07003 -10920924103/12/202107:00:00 AM
Sheet1


I would like a code so that if it finds the same employee ID, it would put the next Punch Time into Punch Time 2, and if it finds another, into Punch Time 3 and so forth.

Book1
ABCDEFG
1EMPLOYEE IDPUNCH TIME 1PUNCH TIME 2PUNCH TIME 3PUNCH TIME 4PUNCH TIME 5PUNCH TIME 6
2
3
4
5
Sheet2


There would be approximately 10,000+ rows, not sure if excel has trouble handling such high volume data and if it would take long?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
VBA Code:
Sub PunchTime()
    Dim shSrc As Worksheet
    Dim shDest As Worksheet
    Dim rngSrc As Range
    Dim rngDest As Range
    Dim lr As Long
    Dim lc As Integer
    Dim c As Range
    Dim sEmp As Variant
    Dim tEmp As Date
    Set shSrc = Worksheets("Sheet1")
    Set shDest = Worksheets("Sheet2")
    lrSrc = shSrc.Range("D" & Rows.Count).End(xlUp).Row
    Set rngSrc = shSrc.Range("D2:D" & lrSrc)
    For Each c In rngSrc
        sEmp = c.Value
        tEmp = c.Offset(0, 5)
        lrDest = shDest.Range("A" & Rows.Count).End(xlUp).Row
        Set rngDest = shDest.Range("A1:A" & lrDest)
        fr = Application.Match(sEmp, rngDest, 0)
        If Not IsError(fr) Then
            lc = shDest.Cells(fr, Columns.Count).End(xlToLeft).Column + 1
            shDest.Cells(fr, lc) = tEmp
        Else
            shDest.Cells(lrDest + 1, 1) = sEmp
            shDest.Cells(lrDest + 1, 2) = tEmp
        End If
    Next
End Sub
 
Upvote 0
Hi there,

Thank you for the quick response, I tried the above code but it is not quite what I want.
The output is still in the column. If possible to have the next punch for the same employee in the next column, which is C and if there is a 3rd or 4th punch, in their respective columns, D and E.

Book1
ABCDEFG
1EMPLOYEE IDPUNCH TIME 1PUNCH TIME 2PUNCH TIME 3PUNCH TIME 4PUNCH TIME 5PUNCH TIME 6
21065589584:16:00 PM
31065589587:37:00 AM
Sheet2
 
Upvote 0
The macro can't match because the datatype in column A must be the same as column D.
What is the datatype of both columns: number or text or ....??
You can change this line: Dim sEmp As Variant --> Dim sEmp As Long
 
Upvote 0
Solution
Is column A "Balance Date" significant or do you just want all the same employee number to have their "Punch Time" to run across the columns.
 
Upvote 0
In this case, date would not matter. Having their punch times in the same row is the main part.

Thank you
 
Upvote 0
The macro can't match because the datatype in column A must be the same as column D.
What is the datatype of both columns: number or text or ....??
You can change this line: Dim sEmp As Variant --> Dim sEmp As Long
Thank you, this worked well..
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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