Dazzawm - Code To Find Duplicates And Highlight.xlsm | |||
---|---|---|---|
C | |||
2 | ASVIR53 5001 | ||
3 | ASVIR53 5001 | ||
4 | AU10018 2001 | ||
5 | AU10018 2001 | ||
6 | asdfg | ||
7 | asdfg | ||
8 | AU10018 2001 | ||
9 | |||
10 | |||
Sheet4 |
Dazzawm - Code To Find Duplicates And Highlight.xlsm | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | AD | AE | AF | |||||||||||||||||||||||||||||
1 | |||||||||||||||||||||||||||||||||
2 | ASVIR53 5001 | Data1 | |||||||||||||||||||||||||||||||
3 | ASVIR53 5001 | Data2 | |||||||||||||||||||||||||||||||
4 | AU10018 2001 | Data1 | |||||||||||||||||||||||||||||||
5 | AU10018 2001 | Data2 | |||||||||||||||||||||||||||||||
6 | AU10018 2001 | Data2 | |||||||||||||||||||||||||||||||
7 | AU10018 2001 | Data1 | |||||||||||||||||||||||||||||||
8 | AU10018 2001 | Data4 | |||||||||||||||||||||||||||||||
9 | |||||||||||||||||||||||||||||||||
Sheet3 |
CorrectI need more info to speed up the code.
I assumed 2 things:
1. that within each group defined by column C, there are no identical items in column C that are positioned apart from each other. Therefore, they shouldn't appear as shown in red for AU10018.
Dazzawm - Code To Find Duplicates And Highlight.xlsm
C 2 ASVIR53 5001 3 ASVIR53 5001 4 AU10018 2001 5 AU10018 2001 6 asdfg 7 asdfg 8 AU10018 2001 9 10 Sheet4
2. that within each group defined by column C, there are no identical items in column AE that are positioned apart from each other. Therefore, they shouldn't appear as shown in red for Data1.
Dazzawm - Code To Find Duplicates And Highlight.xlsm
B C AD AE AF 1 2 ASVIR53 5001 Data1 3 ASVIR53 5001 Data2 4 AU10018 2001 Data1 5 AU10018 2001 Data2 6 AU10018 2001 Data2 7 AU10018 2001 Data1 8 AU10018 2001 Data4 9 Sheet3
Is my assumption correct?
Actually number 2 they may be apart from each other in AE as your example in red.I need more info to speed up the code.
I assumed 2 things:
1. that within each group defined by column C, there are no identical items in column C that are positioned apart from each other. Therefore, they shouldn't appear as shown in red for AU10018.
Dazzawm - Code To Find Duplicates And Highlight.xlsm
C 2 ASVIR53 5001 3 ASVIR53 5001 4 AU10018 2001 5 AU10018 2001 6 asdfg 7 asdfg 8 AU10018 2001 9 10 Sheet4
2. that within each group defined by column C, there are no identical items in column AE that are positioned apart from each other. Therefore, they shouldn't appear as shown in red for Data1.
Dazzawm - Code To Find Duplicates And Highlight.xlsm
B C AD AE AF 1 2 ASVIR53 5001 Data1 3 ASVIR53 5001 Data2 4 AU10018 2001 Data1 5 AU10018 2001 Data2 6 AU10018 2001 Data2 7 AU10018 2001 Data1 8 AU10018 2001 Data4 9 Sheet3
Is my assumption correct?
UsedRange
. My final attempt:Sub test()
Dim d As Object, columnC As Variant, columnAE As Variant, i As Long, j As Long, counter As Long, s As Long
Set d = CreateObject("Scripting.Dictionary")
With Application
columnC = .Transpose(Range("C1:C" & Cells(Rows.Count, 3).End(xlUp).Row))
columnAE = .Transpose(Range("AE1:AE" & Cells(Rows.Count, 31).End(xlUp).Row))
.ScreenUpdating = False
For i = 2 To UBound(columnC)
If columnC(i) = columnC(i - 1) Then
d.Add columnAE(i - 1), 1
If d.Exists(columnAE(i)) Then
counter = 0
For j = s To UBound(columnC)
If columnC(s) = columnC(j) Then
counter = counter + 1
Else
Exit For
End If
Next
Cells(s, 3).Resize(counter).Interior.Color = 65535
d.RemoveAll
i = j
s = i
End If
Else
s = i
d.RemoveAll
End If
Next
.ScreenUpdating = True
End With
End Sub
That worked, just missing the extras that @Akuini is sorting. Thanks again for your efforts.Maybe your sheet didn't likeUsedRange
. My final attempt:
VBA Code:Sub test() Dim d As Object, columnC As Variant, columnAE As Variant, i As Long, j As Long, counter As Long, s As Long Set d = CreateObject("Scripting.Dictionary") With Application columnC = .Transpose(Range("C1:C" & Cells(Rows.Count, 3).End(xlUp).Row)) columnAE = .Transpose(Range("AE1:AE" & Cells(Rows.Count, 31).End(xlUp).Row)) .ScreenUpdating = False For i = 2 To UBound(columnC) If columnC(i) = columnC(i - 1) Then d.Add columnAE(i - 1), 1 If d.Exists(columnAE(i)) Then counter = 0 For j = s To UBound(columnC) If columnC(s) = columnC(j) Then counter = counter + 1 Else Exit For End If Next Cells(s, 3).Resize(counter).Interior.Color = 65535 d.RemoveAll i = j s = i End If Else s = i d.RemoveAll End If Next .ScreenUpdating = True End With End Sub
Sub Dazzawm_5()
'' section group looping
Dim i As Long, j As Long, n As Long
Dim c As Range
Dim tx As String
Dim va, vb, ary, x
Dim d As Object
Dim xFlag As Boolean
Dim t As Double
t = Timer
ActiveSheet.Cells.Interior.Color = xlNone
va = Range("C1", Cells(Rows.Count, "C").End(xlUp))
vb = Range("AE1", Cells(Rows.Count, "AE").End(xlUp))
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
'NDA, NLA, NA and NYA
ary = Split("NDA,NLA,NA,NYA", ",")
For i = 1 To UBound(vb, 1)
For Each x In ary
If vb(i, 1) = x Then vb(i, 1) = Empty: Exit For
Next
Next
For i = 2 To UBound(va, 1)
j = i
tx = va(i, 1)
Do
i = i + 1
If i > UBound(va, 1) Then Exit Do
Loop While va(i, 1) = tx
i = i - 1
If i <> j Then
d.RemoveAll
xFlag = False
For n = j To i
If vb(n, 1) <> "" Then
If Not d.Exists(vb(n, 1)) Then
d(vb(n, 1)) = Empty
Else
xFlag = True
Exit For
End If
End If
Next
If xFlag Then Range(Cells(j, "C"), Cells(i, "C")).EntireRow.Interior.Color = vbYellow
End If
Next
Debug.Print "It's done in: " & Format(Timer - t, "0.00") & " seconds"
End Sub
G | XXNLA | |||||||||||||||||||||||||||
G | XXNLA | |||||||||||||||||||||||||||
J | a | |||||||||||||||||||||||||||
K | b | |||||||||||||||||||||||||||
M | a | |||||||||||||||||||||||||||
M | a | |||||||||||||||||||||||||||
M | b | |||||||||||||||||||||||||||
S | a | |||||||||||||||||||||||||||
S | b | |||||||||||||||||||||||||||
S | c | |||||||||||||||||||||||||||
U | a | |||||||||||||||||||||||||||
U | b | |||||||||||||||||||||||||||
U | a | |||||||||||||||||||||||||||
Q | NLA | |||||||||||||||||||||||||||
Q | NLA | |||||||||||||||||||||||||||
Seems absolutely perfect, I can't thank you enough. FYI the message box saying no duplicates found didn't appear and the timer didn't pop up either. But this is no problem.The usage of the Union function proved to be quite slow.
In its revised version, the process is notably faster compared to the previous one. It completed in just 2 seconds when applied to 150,000 rows of data.
Additionally, I've integrated your new criteria, which involves excluding entries with the values NDA, NLA, NA, and NYA.
VBA Code:Sub Dazzawm_5() '' section group looping Dim i As Long, j As Long, n As Long Dim c As Range Dim tx As String Dim va, vb, ary, x Dim d As Object Dim xFlag As Boolean Dim t As Double t = Timer ActiveSheet.Cells.Interior.Color = xlNone va = Range("C1", Cells(Rows.Count, "C").End(xlUp)) vb = Range("AE1", Cells(Rows.Count, "AE").End(xlUp)) Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare 'NDA, NLA, NA and NYA ary = Split("NDA,NLA,NA,NYA", ",") For i = 1 To UBound(vb, 1) For Each x In ary If vb(i, 1) = x Then vb(i, 1) = Empty: Exit For Next Next For i = 2 To UBound(va, 1) j = i tx = va(i, 1) Do i = i + 1 If i > UBound(va, 1) Then Exit Do Loop While va(i, 1) = tx i = i - 1 If i <> j Then d.RemoveAll xFlag = False For n = j To i If vb(n, 1) <> "" Then If Not d.Exists(vb(n, 1)) Then d(vb(n, 1)) = Empty Else xFlag = True Exit For End If End If Next If xFlag Then Range(Cells(j, "C"), Cells(i, "C")).EntireRow.Interior.Color = vbYellow End If Next Debug.Print "It's done in: " & Format(Timer - t, "0.00") & " seconds" End Sub
G XXNLA G XXNLA J a K b M a M a M b S a S b S c U a U b U a Q NLA Q NLA
Sub test_Transppose_limit()
vb = Application.Transpose(Range("A1:A100000"))
Debug.Print UBound(vb) 'returns: 34464, it's 100000 - 65536
End Sub
Oh, that's interesting. I didn't know that. I used to use in my projects regularly. So what will be the best practice to convert 2D array into a 1D array?@Flashbond
Transpose on an array in VBA, the array must be no more than 65,536 rows long. Otherwise it only get the remaining items and unfortunately without raising an error.
Try this:
VBA Code:Sub test_Transppose_limit() vb = Application.Transpose(Range("A1:A100000")) Debug.Print UBound(vb) 'returns: 34464, it's 100000 - 65536 End Sub
Here's for the message:Seems absolutely perfect, I can't thank you enough. FYI the message box saying no duplicates found didn't appear and the timer didn't pop up either. But this is no problem.
Sub Dazzawm_6()
'' section group looping
Dim i As Long, j As Long, n As Long
Dim c As Range
Dim tx As String
Dim va, vb, ary, x
Dim d As Object
Dim xFlag As Boolean
Dim t As Double
t = Timer
ActiveSheet.Cells.Interior.Color = xlNone
va = Range("C1", Cells(Rows.Count, "C").End(xlUp))
vb = Range("AE1", Cells(Rows.Count, "AE").End(xlUp))
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
'NDA, NLA, NA and NYA
ary = Split("NDA,NLA,NA,NYA", ",")
For i = 1 To UBound(vb, 1)
For Each x In ary
If vb(i, 1) = x Then vb(i, 1) = Empty: Exit For
Next
Next
For i = 2 To UBound(va, 1)
j = i
tx = va(i, 1)
Do
i = i + 1
If i > UBound(va, 1) Then Exit Do
Loop While va(i, 1) = tx
i = i - 1
If i <> j Then
d.RemoveAll
xFlag = False
For n = j To i
If vb(n, 1) <> "" Then
If Not d.Exists(vb(n, 1)) Then
d(vb(n, 1)) = Empty
Else
xFlag = True
Exit For
End If
End If
Next
If xFlag Then Range(Cells(j, "C"), Cells(i, "C")).EntireRow.Interior.Color = vbYellow
End If
Next
If xFlag = True Then
MsgBox "Found duplicate" & vbLf & "It's done in: " & Format(Timer - t, "0.00") & " seconds"
Else
MsgBox "No duplicate found"
End If
End Sub