VBA to Check Duplicate Names and Align Data from Multiple Lines to Just 1 Line

lamarh755

New Member
Joined
Jan 28, 2020
Messages
35
Office Version
  1. 2013
I have a spreadsheet that is exported from Kronos for employee timekeeping. The report often lists the employees name (in Column A) more than once because there are only 2 punches per line. The first punch is the start punch, the next punch is the punch for lunch. The second line normally has the punch back from lunch (if the employee leaves the building), followed by the end of shift punch. There are times were there are more lines for the same employee based on if the employee arrived late, left early, etc. I am trying to create a macro that will do the following...

* Check the number of times the name appears in Column A, line by line and display the value in Column C. So if A3 has a name that appears for the first time, the value in C3 would be 1. If A4 has the same name, the value in C4 would be 2, so for and so on until the end of the sheet. For every new name or non match in Column A, the value in Column C would go back to 1.

* If the value is 1 in Column C, Copy the values showing in Columns E and F of that same row and paste the information in I and J.
* If the value is 2 in Column C, Copy the values showing in Columns E and F of that same row and paste the information in K and L of the same row that has the value of 1 in Column C.
* If the value is 3 in Column C, Copy the values showing in Columns E and F of that same row and paste the information in M and N of the same row that has the value of 1 in Column C.

I am trying to place all of the punches on the same line so that a lookup from another sheet captures all of the punches and puts them in order as well. I already have the macro for the other sheet.
VBA Capture V.PNG
Please see attached example.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this

VBA Code:
Sub AlignData()
  Dim a As Variant, b As Variant
  Dim dic As Object, i As Long, j As Long, k As Long
  '
  Set dic = CreateObject("Scripting.dictionary")
  a = Range("A3:G" & Range("A" & Rows.Count).End(3).Row).Value2
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1))
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      dic(a(i, 1)) = Empty
      j = i
      k = 1
      b(j, k) = a(i, 6)
      b(j, k + 1) = a(i, 7)
      k = k + 2
    Else
      b(j, k) = a(i, 6)
      b(j, k + 1) = a(i, 7)
      k = k + 2
    End If
  Next
  Range("I3").Resize(UBound(a), k).Value = b
End Sub
 
Upvote 0
The macro is copying/pasting F and G instead of E and F (as shown in example 1). I tried to read and understand the code so I could correct the issue. I changed the values for a(i,6) and a(i,7) to a(i,5) and a(i,6) and got it capture the value for E but the value in Column F is not showing up in Column L (as shown in example 2). Only thing that I can think of is that there should be a Dim L As Long, M As Long, etc., but I am not sure.
VBA Capture VII.PNG


VBA Capture VI.PNG
 
Upvote 0
Sorry for that, my mistake.
I put the updated code a little simplified.

VBA Code:
Sub AlignData()
  Dim a As Variant, b As Variant
  Dim dic As Object, i As Long, j As Long, k As Long
  '
  Set dic = CreateObject("Scripting.dictionary")
  a = Range("A3:G" & Range("A" & Rows.Count).End(3).Row).Value2
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1))
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      dic(a(i, 1)) = Empty
      j = i
      k = 1
    End If
    b(j, k) = a(i, 5)
    b(j, k + 1) = a(i, 6)
    k = k + 2
  Next
  Range("I3").Resize(UBound(a), k).Value = b
End Sub
 
Upvote 0
works for me
varios 10abr2020.xlsm
ABCDEFGHIJKLMNOPQR
11234567
2EmployeeDateNoInOutShiftInOutInOutInOutInOutInOut
3all04-sep06:4808:4606:4808:46
4wil05-sep07:4809:4607:4809:4608:4810:46
5wil06-sep08:4810:46
6wil07-sep
7D08-sep01:0001:0001:0001:00
8wra09-sep02:0002:0002:0002:0003:0003:0004:0004:0005:0005:00
9wra10-sep03:0003:00
10wra11-sep04:0004:00
11wra12-sep05:0005:00
Hoja3
Cell Formulas
RangeFormula
E7:F11,E4:F5E4=E3+"1:00"

_______________________________________________________________________________________
- Did you modify something of the macro?
- Do the data in columns A, E or F have spaces to the right or to the left within the cell?
 
Upvote 0
I only copy and pasted the macro. Columns A,E or D do not have any spaces to the right or left. I opened a blank spreadsheet and keyed in data in a few cells and tested the macro and I still can't get data past column K.
 
Upvote 0
Try this

VBA Code:
Sub AlignData()
  Dim a As Variant, b As Variant
  Dim dic As Object, i As Long, j As Long, k As Long
  '
  Set dic = CreateObject("Scripting.dictionary")
  a = Range("A3:F" & Range("A" & Rows.Count).End(3).Row).Value2
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1))
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      dic(a(i, 1)) = Empty
      j = i
      k = 1
    End If
    b(j, k) = a(i, 5)
    b(j, k + 1) = a(i, 6)
    k = k + 2
  Next
  Range("I3").Resize(UBound(a), UBound(b, 2)).Value = b
End Sub
 
Last edited:
Upvote 0
Sorry for the errors in my code, I'm glad it already works for you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,716
Members
449,254
Latest member
Eva146

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