How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells? 2

How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?

VBA, or Visual Basic for Applications, is a programming language commonly used in Microsoft Excel to automate tasks and enhance the functionality of spreadsheets. One useful application of VBA is to apply conditional formatting to identify and highlight duplicate values in a range of cells. This can be achieved by creating a macro that compares the values in each cell and applies a formatting rule to highlight any duplicates. This process can save time and effort for users who need to identify and manage duplicate data in their spreadsheets. By utilizing VBA, users can easily customize the formatting rules and apply them to different ranges of cells, making it a versatile tool for efficiently managing data in Excel.

VBA: Apply Conditional Formatting to Duplicate Values


You can use the following basic syntax in VBA to apply conditional formatting to duplicate values in a specific range:

Sub ConditionalFormatDuplicates()

Dim rg As Range
Dim uv As UniqueValues

'specify range to apply conditional formatting
Set rg = Range("A2:A11")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'identify duplicate values in range A2:A11
Set uv = rg.FormatConditions.AddUniqueValues
uv.DupeUnique = xlDuplicate

'apply conditional formatting to duplicate values
uv.Interior.Color = vbBlue
uv.Font.Color = vbWhite
uv.Font.Bold = True

End Sub

This particular example applies conditional formatting to duplicate values in the range A2:A11 of the current sheet in Excel.

The following example shows how to use this syntax in practice.

Example: Use VBA to Apply Conditional Formatting to Duplicate Values

Suppose we have the following column of values in Excel:

Suppose we would like to apply the following conditional formatting to duplicate values in column A:

  • Blue background
  • Black text
  • Bold text

We can create the following macro to do so:

Sub ConditionalFormatDuplicates()

Dim rg As Range
Dim uv As UniqueValues

'specify range to apply conditional formatting
Set rg = Range("A2:A11")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'identify duplicate values in range A2:A11
Set uv = rg.FormatConditions.AddUniqueValues
uv.DupeUnique = xlDuplicate

'apply conditional formatting to duplicate values
uv.Interior.Color = vbBlue
uv.Font.Color = vbWhite
uv.Font.Bold = True

End Sub

When we run this macro, we receive the following output:

Notice that conditional formatting is applied to each cell in column A with a duplicate value.

If you would like to apply conditional formatting to a different range of cells, simply change A2:A11 in the macro to a different range.

Also, if you’d like to remove all conditional formatting from cells in the current sheet, you can create the following macro to do so:

Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

Notice that all conditional formatting has been removed from the cells.

Cite this article

stats writer (2024). How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-apply-conditional-formatting-to-identify-and-highlight-duplicate-values-in-a-range-of-cells/

stats writer. "How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-apply-conditional-formatting-to-identify-and-highlight-duplicate-values-in-a-range-of-cells/.

stats writer. "How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-apply-conditional-formatting-to-identify-and-highlight-duplicate-values-in-a-range-of-cells/.

stats writer (2024) 'How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-apply-conditional-formatting-to-identify-and-highlight-duplicate-values-in-a-range-of-cells/.

[1] stats writer, "How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use VBA to apply conditional formatting to identify and highlight duplicate values in a range of cells?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top