Highlight the most repeated value in columns

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
75
Office Version
  1. 2019
Platform
  1. Windows
Hi guys. I need some help please :)

I need VBA or conditional formatting formula to highlight most repeated values in each column. like shown on screenshot below.
I did this manually for the first three columns just to show example. So in column B the most repeated value is 30. In column C -5. And in column D -24 and -20.
I have a lot of columns to process... How can i do this the fastest way.
 

Attachments

  • Clipboard01.png
    Clipboard01.png
    80.1 KB · Views: 11
I think this should work in xl2021. You might have trouble with blanks in your thing depending on how you implement it.

MrExcelPlayground24.xlsx
BCDEFG
21133FALSEFALSE
31235FALSEFALSE
4134FALSEFALSE
5242FALSEFALSE
6254FALSEFALSE
7263FALSEFALSE
8371TRUETRUE
9382TRUETRUE
103TRUETRUE
113TRUETRUE
124FALSEFALSE
134FALSEFALSE
145TRUETRUE
155TRUETRUE
165TRUETRUE
175TRUETRUE
186FALSEFALSE
196FALSEFALSE
206FALSEFALSE
217FALSEFALSE
228FALSEFALSE
238FALSEFALSE
Sheet6
Cell Formulas
RangeFormula
C2:C9C2=UNIQUE(B2:B23)
D2:D9D2=MMULT(--(TOROW(B2:B23)=C2#),SEQUENCE(ROWS(B2:B23),1,1,0))
E2:E3E2=FILTER(C2#,D2#=MAX(D2#))
F2:F23F2=ISNUMBER(XMATCH(B2:B23,E2#,0))
G2:G23G2=LET(x,B$2:B$23,y,B2,a,UNIQUE(x),b,MMULT(--(TOROW(x)=a),SEQUENCE(ROWS(x),1,1,0)),c,FILTER(a,b=MAX(b)),d,ISNUMBER(XMATCH(y,c,0)),d)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:B23Expression=LET(x,B$2:B$23,y,B2,a,UNIQUE(x),b,MMULT(--(TOROW(x)=a),SEQUENCE(ROWS(x),1,1,0)),c,FILTER(a,b=MAX(b)),d,ISNUMBER(XMATCH(y,c,0)),d)textNO
 
Upvote 0
I think this should work in xl2021. You might have trouble with blanks in your thing depending on how you implement it.
I tried this formula but it doesnt work. I get this error in screenshot below. Maybe it works in excel 2021 but im using 2019. Im sorry i forgot to change this in my account settings...
 

Attachments

  • Clipboard02.png
    Clipboard02.png
    46.1 KB · Views: 6
Upvote 0
Another approach,
VBA Code:
Sub HighlightMostRepeated()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim r As Long, c As Long
    Dim dict As Object
    Dim key As Variant
    Dim maxCount As Integer
    Dim cellValue As String
   
    Set ws = ActiveSheet
    lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
    Application.ScreenUpdating = False
    For c = 1 To lastCol
        Set dict = CreateObject("Scripting.Dictionary")
        For r = 1 To lastRow
            cellValue = Trim(ws.Cells(r, c).Value)
            If cellValue <> "" Then
                If dict.exists(cellValue) Then
                    dict(cellValue) = dict(cellValue) + 1
                Else
                    dict.Add cellValue, 1
                End If
            End If
        Next r
        maxCount = 0
        For Each key In dict.keys
            If dict(key) > maxCount Then maxCount = dict(key)
        Next key
        For r = 1 To lastRow
            cellValue = Trim(ws.Cells(r, c).Value)
            If dict.exists(cellValue) Then
                If dict(cellValue) = maxCount Then
                    ws.Cells(r, c).Interior.Color = RGB(255, 0, 0)
                   
                End If
            End If
        Next r
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another approach,
VBA Code:
Sub HighlightMostRepeated()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim r As Long, c As Long
    Dim dict As Object
    Dim key As Variant
    Dim maxCount As Integer
    Dim cellValue As String
  
    Set ws = ActiveSheet
    lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
    Application.ScreenUpdating = False
    For c = 1 To lastCol
        Set dict = CreateObject("Scripting.Dictionary")
        For r = 1 To lastRow
            cellValue = Trim(ws.Cells(r, c).Value)
            If cellValue <> "" Then
                If dict.exists(cellValue) Then
                    dict(cellValue) = dict(cellValue) + 1
                Else
                    dict.Add cellValue, 1
                End If
            End If
        Next r
        maxCount = 0
        For Each key In dict.keys
            If dict(key) > maxCount Then maxCount = dict(key)
        Next key
        For r = 1 To lastRow
            cellValue = Trim(ws.Cells(r, c).Value)
            If dict.exists(cellValue) Then
                If dict(cellValue) = maxCount Then
                    ws.Cells(r, c).Interior.Color = RGB(255, 0, 0)
                  
                End If
            End If
        Next r
    Next c
    Application.ScreenUpdating = True
End Sub
VBA code runs fine no errors no nothing. But cells with most repeated values are not highlighted in red "RGB(255, 0, 0)" What im doing wrong? Can you check this out...

Here is the link to worksheet im working with. Try it yourself.
 
Upvote 0
Please try this,
VBA Code:
Sub HighlightMostRepeated()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim r As Long, c As Long
    Dim dict As Object
    Dim key As Variant
    Dim maxCount As Integer
    Dim cellValue As String
    
    Set ws = ActiveSheet
    lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column

    Application.ScreenUpdating = False

    ws.Cells.Interior.ColorIndex = xlNone

    For c = 1 To lastCol
        Set dict = CreateObject("Scripting.Dictionary")
        
        For r = 1 To lastRow
            cellValue = Trim(CStr(ws.Cells(r, c).Value))
            If cellValue <> "" Then
                If dict.exists(cellValue) Then
                    dict(cellValue) = dict(cellValue) + 1
                Else
                    dict.Add cellValue, 1
                End If
            End If
        Next r
        
        
        maxCount = 0
        For Each key In dict.keys
            If dict(key) > maxCount Then maxCount = dict(key)
        Next key
        
        
        For r = 1 To lastRow
            cellValue = Trim(CStr(ws.Cells(r, c).Value))
            If dict.exists(cellValue) Then
                If dict(cellValue) = maxCount Then
                    ws.Cells(r, c).Interior.ColorIndex = 3
                End If
            End If
        Next r
    Next c

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
You could try this Conditional Formatting. Note that nothing would be highlighted if all entries in the column are unique.

25 03 06.xlsm
ABCD
1
2-10-1
300-1
410-1
521-1
6220
7220
8341
9341
10352
11352
12352
13352
14463
15563
16563
17564
18565
19585
20685
21795
22796
238108
248109
258109
2681010
2791010
28101110
29101211
30111311
31121312
32121413
33131413
34131513
35141514
36151514
37161515
38171515
39171515
40171616
41171616
42181717
43
CF Most
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:AJ42Expression=COUNTIF(B$2:B$42,B2)=COUNTIF(B$2:B$42,MODE(B$2:B$42))textNO
 
Last edited:
Upvote 0
Solution
You could try this Conditional Formatting. Note that nothing would be highlighted if all entries in the column are unique.

25 03 06.xlsm
ABCD
1
2-10-1
300-1
410-1
521-1
6220
7220
8341
9341
10352
11352
12352
13352
14463
15563
16563
17564
18565
19585
20685
21795
22796
238108
248109
258109
2681010
2791010
28101110
29101211
30111311
31121312
32121413
33131413
34131513
35141514
36151514
37161515
38171515
39171515
40171616
41171616
42181717
43
CF Most
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:AJ42Expression=COUNTIF(B$2:B$42,B2)=COUNTIF(B$2:B$42,MODE(B$2:B$42))textNO
Awesome. This does exactly what i need... Thanks a lot man...

Please try this,
VBA Code:
Sub HighlightMostRepeated()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim r As Long, c As Long
    Dim dict As Object
    Dim key As Variant
    Dim maxCount As Integer
    Dim cellValue As String
   
    Set ws = ActiveSheet
    lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column

    Application.ScreenUpdating = False

    ws.Cells.Interior.ColorIndex = xlNone

    For c = 1 To lastCol
        Set dict = CreateObject("Scripting.Dictionary")
       
        For r = 1 To lastRow
            cellValue = Trim(CStr(ws.Cells(r, c).Value))
            If cellValue <> "" Then
                If dict.exists(cellValue) Then
                    dict(cellValue) = dict(cellValue) + 1
                Else
                    dict.Add cellValue, 1
                End If
            End If
        Next r
       
       
        maxCount = 0
        For Each key In dict.keys
            If dict(key) > maxCount Then maxCount = dict(key)
        Next key
       
       
        For r = 1 To lastRow
            cellValue = Trim(CStr(ws.Cells(r, c).Value))
            If dict.exists(cellValue) Then
                If dict(cellValue) = maxCount Then
                    ws.Cells(r, c).Interior.ColorIndex = 3
                End If
            End If
        Next r
    Next c

    Application.ScreenUpdating = True
   
End Sub
Still not working. But i doesnt matter i will use formula. Anyway thanks for your effort.

Have a nice day guys...
 
Upvote 0

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