VBA - Highlight and format the duplicate value found in a column of data

Kerryx

Well-known Member
Joined
May 6, 2016
Messages
718
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
When the code runs it asks the user to input the range to check in this example $I$1:$I$21

I have manually entered in the countif formula the "i" value so it reads COUNTIF(i$1:i1,i1)

IS there a way for the formula to take the value form the input $I$1:$I$21 and use it in my countif formula or will i need to use a second input?

The code is not pretty but i am doing my best, sorry if it offends the pro's out there :unsure:

VBA Code:
Dim rng As Range
Dim cf As FormatCondition
On Error Resume Next
Set rng = Application.InputBox(prompt:="Enter the range to check", Title:="Cell Reference", Type:=8)
On Error GoTo 0
'Check for Cancel button
If rng Is Nothing Then Exit Sub
Set rng = rng
Set cf = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=COUNTIF(i$1:i1,i1)>1")
cf.Interior.Color = vbRed
cf.NumberFormat = "0.00"

this is what i am trying to accomplish

duplicate.JPG
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi ... Try the below code

VBA Code:
Sub Test()

Dim rng As Range, cf As FormatCondition
On Error Resume Next
Set rng = Application.InputBox(prompt:="Enter the range to check", Title:="Cell Reference", Type:=8)
'On Error GoTo 0
'Check for Cancel button
If rng Is Nothing Then Exit Sub
Set rng = rng
Set cf = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=COUNTIF(" & rng.Address & "," & rng.Cells(1).Address(0, 0) & ")>1")
cf.Interior.Color = vbRed
cf.NumberFormat = "0.00"

End Sub
 
Upvote 0
Hi thanks for this almost right, in the countif formula it is formatting all instances of the duplicate values instead of the second instance of the value.
 

Attachments

  • test.JPG
    test.JPG
    55.7 KB · Views: 7
Upvote 0
Ok, try now

VBA Code:
Sub Test()

Dim rng As Range, cf As FormatCondition
On Error Resume Next
Set rng = Application.InputBox(prompt:="Enter the range to check", Title:="Cell Reference", Type:=8)
'On Error GoTo 0
'Check for Cancel button
If rng Is Nothing Then Exit Sub
Set rng = rng
With rng
  Set cf = .FormatConditions.Add(Type:=xlExpression, Formula1:= _
    "=COUNTIF(" & .Cells(1).Address & ":" & .Cells(1).Address(0, 0) & "," & .Cells(1).Address(0, 0) & ")>1")
End With
cf.Interior.Color = vbRed
cf.NumberFormat = "0.00"

End Sub
 
Upvote 0
Thank you so much this worked a treat can you explain the formula structure if you have the time .
=COUNTIF(" & .Cells(1).Address & ":" & .Cells(1).Address(0, 0) & "," & .Cells(1).Address(0, 0) & ")>1")
This is great thank you again
 
Upvote 0
Glad to help :)

You have defined your range rng by the InputBox and .Cells(1) means the first cell of the range and the .Address returns "$A$1" and I used .Address(0,0) to ignore absolute reference (returns "A1")

Run the below code to see what's the output of the formula
VBA Code:
Sub TestAddress()

Dim rng As Range

Set rng = Application.InputBox(prompt:="Enter the range to check", Title:="Cell Reference", Type:=8)
With rng.Cells(1)
  MsgBox "=COUNTIF(" & .Address & ":" & .Address(0, 0) & "," & .Address(0, 0) & ")>1"
End With

End Sub

You can read more about .Address HERE
 
Upvote 0
Really appreciate the help and the education , thank you(y)
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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