nwaughachukwuma

Untitled

Feb 27th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. Sub sbHighlightDuplicatesInColumn()
  2. Dim lastRow As Long
  3. Dim matchFoundIndex As Long
  4. Dim iCntr As Long
  5.  
  6. lastRow = Sheets("Sheet1").Range("A1").SpecialCells(xlCellTypeLastCell).Row
  7. For iCntr = 2 To lastRow
  8. If Cells(iCntr, 1) <> "" Then
  9. matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range(Cells(1, 1), Cells(iCntr, 1)), 0)
  10. If iCntr <> matchFoundIndex Then
  11. Sheets("Sheet1").Cells(iCntr, 1).Interior.Color = vbYellow
  12. Else
  13. Sheets("Sheet1").Cells(iCntr, 1).Interior.Color = vbGreen
  14. End If
  15. End If
  16. Next
  17.  
  18. 'iterating over the 2 columns...
  19.  
  20. numOfRows = ActiveWorkbook.Worksheets("Sheet1").Range("A2", Worksheets("Sheet1").Range("A2").End(xlDown)).Rows.Count
  21. freq = numOfRows / 3
  22.  
  23. Dim lastRowH As Long
  24. lastRowL = Sheets("Sheet1").Range("H1").SpecialCells(xlCellTypeLastCell).Row
  25.  
  26. Dim LastRowI As Long
  27. LastRowM = Sheets("Sheet1").Range("I1").SpecialCells(xlCellTypeLastCell).Row
  28.  
  29. Dim rg1 As Range, rg2 As Range
  30. Set rg1 = ActiveWorkbook.Worksheets("Sheet1").Range("A2:A4")
  31. Set rg2 = ActiveWorkbook.Worksheets("Sheet1").Range("G2:G4")
  32.  
  33. ' Create dynamic array
  34. Dim tmpArray1 As Variant, tempArray2 As Variant
  35. Dim code As Variant, value As Variant
  36.  
  37. 'Dump the range into a 2D array
  38. tmpArray1 = rg1.value
  39. tmpArray2 = rg2.value
  40.  
  41. 'Resize the 1D array
  42. ReDim code(1 To UBound(tmpArray1, 1))
  43. ReDim value(1 To UBound(tmpArray2, 1))
  44.  
  45. 'Convert 2D to 1D
  46. For i = 1 To UBound(code, 1)
  47. code(i) = tmpArray1(i, 1)
  48. value(i) = tmpArray2(i, 1)
  49. Next
  50.  
  51. For cnt = 1 To freq
  52. 'iterate over col-H
  53. Dim u As Integer, v As Integer
  54. u = cnt * 3 + 2
  55. v = u + 2
  56.  
  57. Dim iTrack As Integer
  58. iTrack = 1
  59. 'iterate over col-H
  60. For iCntr = u To v
  61. If Cells(iCntr, 8) <> "" Then
  62.  
  63. 'matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 8), Range(Cells(1, 8), Cells(iCntr, 8)), 0)
  64. matchFoundValue = Cells(iCntr, 8).value
  65. If value(iTrack) <> matchFoundValue Then
  66. Sheets("Sheet1").Cells(iCntr, 8).Interior.Color = vbYellow
  67. Else
  68. Sheets("Sheet1").Cells(iCntr, 8).Interior.Color = vbGreen
  69. End If
  70. End If
  71. iTrack = iTrack + 1
  72. Next
  73.  
  74. iTrack = 1
  75. 'iterate over col-I
  76. For iCntr = u To v
  77. If Cells(iCntr, 9) <> "" Then
  78. 'matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 9), Range(Cells(1, 9), Cells(iCntr, 9)), 0)
  79.  
  80. matchFoundValue = Cells(iCntr, 9).value
  81. If code(iTrack) <> matchFoundValue Then
  82. Sheets("Sheet1").Cells(iCntr, 9).Interior.Color = vbRed
  83. Else
  84. Sheets("Sheet1").Cells(iCntr, 9).Interior.Color = vbGreen
  85. End If
  86. End If
  87. iTrack = iTrack + 1
  88. Next
  89. Next
  90.  
  91. End Sub
  92. Private Sub Worksheet_Change(ByVal Target As Range)
  93.  
  94. Dim curColor As Variant
  95. curColor = ActiveCell.Interior.Color
  96. If Application.CountIf(Range("A:A"), Target) > 1 Then
  97. MsgBox "Duplicate Data", vbCritical, "Remove Data"
  98. Target.value = ""
  99. 'ActiveCell.Offset(RowOffset:=-1).EntireRow.Interior.Color = curColor
  100. End If
  101. End Sub
  102.  
  103. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  104.  
  105. End Sub
Advertisement
Add Comment
Please, Sign In to add comment