Written by Allen Wyatt (last updated November 18, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Connie has a worksheet that has company names in each cell of column B. They are grouped under a region heading (Northeast, West, etc.) in column A. She would like to apply conditional formatting to the company names so that if a name appears in more than one region, it shows up using a background or text color that makes the matching companies easy to find. This means that if one company is formatted as red, no other company should appear as red (it should appear as a different color, such as blue or green). Connie isn't sure how to set this up or if it can even be done with conditional formatting.
There is a way to mark duplicates using conditional formatting; just follow these general steps:
Figure 1. The New Formatting Rule dialog box.
At this point all your duplicates should match whatever formatting you selected in step 6. The only problem is that all duplicates are formatted the same way. In other words, if you have two companies (ABC Company and DEF Company) and there are duplicates for those companies, they are all formatted the same way—you won't see different formatting for the two companies.
Of course, you could easily use Excel's filtering capabilities to single out duplicate companies, non-duplicate companies, or individual company names. This might be the easiest way to "zero in" on the companies you want to locate.
The only way to use conditional formatting to apply different colors to different groups of duplicate company names requires that you identify, up front, the actual duplicates. With that list in hand, you could create a series of conditional formatting rules that use formulas similar to the following:
=AND(ISNUMBER(FIND("ABC Company",B1)),COUNTIF($B$1:$B$99,"ABC Company")>1)
In this formula "ABC Company" is the company name, B1 is the first cell of the range, and B1:B99 is the full range of cells. For each formatting rule you could apply different formatting appropriate to that particular company. That means that if you knew, up front, that there were 24 different company names that had duplicates, you would need to set up 24 conditional formatting rules to handle those 24 names.
Complex, indeed. Unfortunately, there is not an easier way using conditional formatting. You could, however, forego the conditional formatting and use a macro to make your duplicates stand out. The simplest "automatic" macro we could come up with (where you don't need to know the duplicate names ahead of time) is one that examines a range of cells and sets the internal cell color based on duplicate company names.
Sub ColorCompanyDuplicates() Dim x As Integer Dim y As Integer Dim lRows As Long Dim lColNum As Long Dim iColor As Integer Dim iDupes As Integer Dim bFlag As Boolean lRows = Selection.Rows.Count lColNum = Selection.Column iColor = 2 For x = 2 To lRows bFlag = False For y = 2 To x - 1 If Cells(y, lColNum) = Cells(x, lColNum) Then bFlag = True Exit For End If Next y If Not bFlag Then iDupes = 0 For y = x + 1 To lRows If Cells(y, lColNum) = Cells(x, lColNum) Then iDupes = iDupes + 1 End If Next y If iDupes > 0 Then iColor = iColor + 1 If iColor > 56 Then MsgBox "Too many duplicate companies!", vbCritical Exit Sub End If Cells(x, lColNum).Interior.ColorIndex = iColor For y = x + 1 To lRows If Cells(y, lColNum) = Cells(x, lColNum) Then Cells(y, lColNum).Interior.ColorIndex = iColor End If Next y End If End If Next x End Sub
To use the macro, simply select the cells that contain the company names and then run it. The macro makes three passes through the cells. The first pass looks backward through the cells from the current one being examined; it is used to determine if there are any "backward" duplicates, because if there are then no further processing on that particular cell is needed. The second pass looks forward through the cells to determine if there are any duplicates to the current company name. If there are, then a third pass increments the cell color value and then applies it to the duplicates.
Note that the macro sets the ColorIndex property of any duplicates it finds, and it increments the variable used to set the property when it finds a new set of duplicate company names. For all those company names for which there are no duplicates, the ColorIndex property of the cell is not changed. This means there is a limit on how many companies can be marked, however—the ColorIndex can only range between 0 and 56. The values actually assigned by the macro range from 3 to 56, so it is only possible to format 54 groupings of companies.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12673) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
When preparing a report for others to use, it is not unusual to add a horizontal line between major sections of the ...
Discover MoreConditional Formatting is a great boon to effectively displaying the information in your worksheets. If you want to ...
Discover MoreWhen you paste information into a row that is conditionally formatted, you may end up messing up the rules applied to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-04-18 06:11:21
Mike J
ASAP Utilities, which has had a few mentions before, contains a utility that can identify up to (approx.) 256 duplicate groups by using a combination of fill colours and patterns. If the number of groups exceeds its maximum, the duplicates are still found but the excess ones are all formatted with the same colour, so selecting/copying that group and repeating the process allows a virtually unlimited number of groups. Definitely worth a mention, as ASAP Utilities has a free version.
2018-07-16 08:40:27
Roy
If you know the companies ahead of time (not the dupes aspect, just a list of all possiblee companies) and there are less than, what, 56?, you could have a table of them with an assigned color which you use VLOOKUP on in the conditional formatting.
The same color would always be assigned for a given company (in the simple version) which could be beneficial if it would be useful to a user to remember certain companies' colors.
For a more complex version, one could make the table dynamic by it collecting the companies in use at the moment and having the formula that populates the index (company name) column "look above" to see if it already exists, leaving the line in the table blank if it does. Similarly for the output column (colors): leave a blank if the index column is blank. The colors would then be somewhat dynamic as well if that is desirable, but more important would be that a larger number of companies could be handled so long as the number with dupes did not exceed the number of possible colors (56?).
Too bad we don't have formulas that directly format. For example, one might then have a formula like =if(A1=2,AND("That's a winner!",FILL(A1,[Red],TEXTCOLOR(A1,[Yellow])),AND("Oh... Sorry!",,FILL(A1,[Light Blue],TEXTCOLOR(A1,[Red]))).
No more screwing with a horrible, non-intuitive, one might even say "broken" given its foibles, conditional formatting.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments