VBA: Formatting the first duplicate of each set of duplicates in a column

Shatnerfreude

New Member
Joined
Feb 26, 2015
Messages
12
Hello,

This is my second posting for this problem... I apologize for "spamming".

I would please like a VB code for making the first duplicate black font in each group of duplicates in a column. I will set the rest of the column to white font as default. (See Example)
This is to avoid merging these duplicate cells, and could be very useful for others, as well. I currently have a code for automatically merging and top justifying sets of duplicates down the column (see below). Maybe it can be used as a template/starting place for What I need?

I have tried many combinations of conditional formatting, but nothing seems to work (although I have learned how to format the bottom duplicate).

Example:
Start with
a
a
a
a
b
b
b
c
c
c
c

Becomes
a
a
a
a

b
b
b

c
c
c
c


(Pretend the faded letters are invisible as white font)

Here is the code which automatically merges and top justifies groups of duplicates. I want to emulate the results of this without merging cells.

Sub MergeDupes()
Dim r As Range, c As Range
Dim i As Long, j As Long
Set r = Range("A1", Cells(Rows.Count, "A").End(xlUp))
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To r.Count
Set c = r(i)
j = 0
Do Until c <> c.Offset(rowoffset:=1)
Set c = c(2)
j = j + 1
Loop
With Range(r(i), c)
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
End With
i = i + j
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

THANK YOU!!!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
If your data start "A2" then try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG08May50
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]For[/COLOR] n = Lst To 2 [COLOR="Navy"]Step[/COLOR] -1
    [COLOR="Navy"]With[/COLOR] Range("A" & n)
        [COLOR="Navy"]If[/COLOR] Not .Offset(-1).Value = .Value [COLOR="Navy"]Then[/COLOR]
            .Font.Bold = True
        [COLOR="Navy"]Else[/COLOR]
            .Font.ColorIndex = 2
        [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]End[/COLOR] With
 [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,
Thank you so much! It wasn't exactly what I needed, but I fixed it myself.
This made it do what I wanted:

Code:
Sub MG08May50()
Dim Lst As Long, n As Long
Lst = Range("A" & Rows.Count).End(xlUp).Row
For n = Lst To 2 Step -1
    With Range("A" & n)
        If Not .Offset(-1).Value = .Value Then
            .Font.Bold = False
        Else
            .Font.ColorIndex = 2
        End If
 End With
 Next n
End Sub
 
Upvote 0
Just out of interest, this should do it with conditional formatting....

Assuming a header row
Rule 1 for format of bold
=COUNTIF($A$2:$A2,A2)=1

Rule 2 for format white font
=COUNTIF($A$2:$A2,A2)>1
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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