norcorf
06-27-2024, 08:43 AM
Hi together, I am new here and that is my first question. I created the folowing macro and got stucked. So I need to indetify duplicates in column A and rearrange them in separate columns starting from the column left to column B according to a defined layout. If I find a new unique duplicate, macro should go back to the first column left to the column B und this column should be used as an insertion point. Sublass column is simplified for a better overview. Duplicates values are grouped always together in sequential rows and are unique.
Here is initial data:
Complaint
Name
Subclass
624
XX
A
624
XX
B
624
XX
C
123
YY
A
123
YY
B
789
XY
A
256
ZZ
A
256
ZZ
B
256
ZZ
C
Here is how it should look like:
Complaint
Name
Subclass
624
XX
A
624
624
XX
B
624
624
XX
C
123
YY
A
123
123
YY
B
789
XY
A
256
ZZ
A
256
256
ZZ
B
256
256
ZZ
C
That means all duplicates of the same occurrences should be placed in one column (2nd together, 3rd together and so on). However, I get a new column every time it finds a duplicate (even if it has a new value) and this column shifts to the left.
Here is VBA I created:
Sub testworking()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Tabelle2")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
Dim value As Variant
Dim colIndex As Long
Dim colDict As Object
Set colDict = CreateObject("Scripting.Dictionary")
For i = 1 To lastRow
value = ws.Cells(i, 1).value
If dict.Exists(value) Then
colIndex = colDict(value) - 1 ' Adjust to insert left to B
If colIndex < 2 Then ' Ensure we do not go beyond column A
colIndex = 2
End If
ws.Columns(colIndex).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ws.Cells(i, colIndex).value = value
Else
dict.Add value, 1
colDict.Add value, ws.Cells(i, 1).Column ' Store the initial column index
End If
Next i
End Sub
I used loop because the dataset is not constant, and the number of duplicates may vary.
Here is a dataset I got after macro has been performed. It is shifting every time it has found a duplicate (even if it is a new unique value):
Complaint
Name
Subclass
624
XX
A
624
624
XX
B
624
624
XX
C
123
YY
A
123
123
YY
B
789
XY
A
256
ZZ
A
256
256
ZZ
B
256
256
ZZ
C
I appreciate any help :)
Here is initial data:
Complaint
Name
Subclass
624
XX
A
624
XX
B
624
XX
C
123
YY
A
123
YY
B
789
XY
A
256
ZZ
A
256
ZZ
B
256
ZZ
C
Here is how it should look like:
Complaint
Name
Subclass
624
XX
A
624
624
XX
B
624
624
XX
C
123
YY
A
123
123
YY
B
789
XY
A
256
ZZ
A
256
256
ZZ
B
256
256
ZZ
C
That means all duplicates of the same occurrences should be placed in one column (2nd together, 3rd together and so on). However, I get a new column every time it finds a duplicate (even if it has a new value) and this column shifts to the left.
Here is VBA I created:
Sub testworking()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Tabelle2")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
Dim value As Variant
Dim colIndex As Long
Dim colDict As Object
Set colDict = CreateObject("Scripting.Dictionary")
For i = 1 To lastRow
value = ws.Cells(i, 1).value
If dict.Exists(value) Then
colIndex = colDict(value) - 1 ' Adjust to insert left to B
If colIndex < 2 Then ' Ensure we do not go beyond column A
colIndex = 2
End If
ws.Columns(colIndex).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ws.Cells(i, colIndex).value = value
Else
dict.Add value, 1
colDict.Add value, ws.Cells(i, 1).Column ' Store the initial column index
End If
Next i
End Sub
I used loop because the dataset is not constant, and the number of duplicates may vary.
Here is a dataset I got after macro has been performed. It is shifting every time it has found a duplicate (even if it is a new unique value):
Complaint
Name
Subclass
624
XX
A
624
624
XX
B
624
624
XX
C
123
YY
A
123
123
YY
B
789
XY
A
256
ZZ
A
256
256
ZZ
B
256
256
ZZ
C
I appreciate any help :)