vba how to find duplicate value in different column in 1 sheet

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
142
Office Version
  1. 2021
Platform
  1. Windows
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect

Dim ws As Worksheet

Dim ColorRng As Range
Dim ColorCell As Range

Set ws = Worksheets("invdatabase")
Set ColorRng = ws.Range("B2", "H10000")

For Each ColorCell In ColorRng
If WorksheetFunction.CountIf(ColorRng, ColorCell.Value) > 1 Then

ColorCell.Interior.Color = RGB(255, 153, 0)
ColorCell.Font.Color = vbBlack

Else

ColorCell.Interior.ColorIndex = 1
ColorCell.Font.Color = vbWhite

End If
Next
 ActiveSheet.Protect
End Sub

these codes find duplicate in column B only, But I need to assign multiple columns like b or c or anyone and it should search duplicate and highlight in its individual column.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect

Dim ws As Worksheet

Dim ColorRng As Range
Dim ColorCell As Range

Set ws = Worksheets("invdatabase")
Set ColorRng = ws.Range("B2", "H10000")

For Each ColorCell In ColorRng
If WorksheetFunction.CountIf(ColorRng, ColorCell.Value) > 1 Then

ColorCell.Interior.Color = RGB(255, 153, 0)
ColorCell.Font.Color = vbBlack

Else

ColorCell.Interior.ColorIndex = 1
ColorCell.Font.Color = vbWhite

End If
Next
 ActiveSheet.Protect
End Sub

these codes find duplicate in column B only, But I need to assign multiple columns like b or c or anyone and it should search duplicate and highlight in its individual column
if i increase range from ("b2:b10000000") than excel get hang why? and ws.range i wrote wrong. actually it is ws.range("B2:B1000000")
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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