How do I write a macro to Conditional Format a cell range?

User avatar
geecee
3StarLounger
Posts: 357
Joined: 02 Jun 2013, 05:55
Location: Australia - SOUTH MORANG - A northern suburb of the city of MELBOURNE in the state of Victoria

How do I write a macro to Conditional Format a cell range?

Post by geecee »

I have a Range("J15:AA15") containing numbers.

What I wish to do is create a macro to detect duplicates.

Using
ScreenShot042.jpg
I would like the result to be like
ScreenShot043.jpg
Any help would be appreciated.

Thanks.
You do not have the required permissions to view the files attached to this post.
George

When we're gaun up the hill o’ fortune, may we ne'er meet a frien' comin' doun!
(When we are going up the hill of fortune, may we never meet a friend coming down!)

:smile: Don't cry because it's over...Smile because it happened.l :smile:

:note: At the end of the day it's midnight. :note:


User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: How do I write a macro to Conditional Format a cell rang

Post by Rudi »

Why do you want a macro?
Conditional formatting will dynamically highlight duplicates if it is set on the range you refer to?

Please explain the reason for a macro request?
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How do I write a macro to Conditional Format a cell rang

Post by HansV »

I recorded a macro, then cleaned it up a bit:

Code: Select all

Sub HighlightDuplicates()
    With Range("J15:AA15").FormatConditions
        .Delete
        .AddUniqueValues
        With .Item(1)
            .DupeUnique = xlDuplicate
            .Font.Color = -16383844
            .Interior.Color = 13551615
            With .Borders(xlLeft)
                .LineStyle = xlContinuous
                .Color = -16383844
                .Weight = xlThin
            End With
            With .Borders(xlRight)
                .LineStyle = xlContinuous
                .Color = -16383844
                .Weight = xlThin
            End With
            With .Borders(xlTop)
                .LineStyle = xlContinuous
                .Color = -16383844
                .Weight = xlThin
            End With
            With .Borders(xlBottom)
                .LineStyle = xlContinuous
                .Color = -16383844
                .Weight = xlThin
            End With
        End With
    End With
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: How do I write a macro to Conditional Format a cell rang

Post by Rudi »

    
...and this is Hans's macro taken to the cleaners. :grin:
I had some time to spare (and felt like experimenting with it!)
Spoiler

Code: Select all

Sub HighlightDuplicates()
    With Range("J15:AA15").FormatConditions
        .Delete
        .AddUniqueValues
        With .Item(1)
            .DupeUnique = xlDuplicate
            .Font.Color = -16383844
            .Interior.Color = 13551615
            .Borders(xlLeft).Color = -16383844
            .Borders(xlRight).Color = -16383844
            .Borders(xlTop).Color = -16383844
            .Borders(xlBottom).Color = -16383844
        End With
    End With
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How do I write a macro to Conditional Format a cell rang

Post by HansV »

Thanks, Rudi.
Best wishes,
Hans

User avatar
geecee
3StarLounger
Posts: 357
Joined: 02 Jun 2013, 05:55
Location: Australia - SOUTH MORANG - A northern suburb of the city of MELBOURNE in the state of Victoria

Re: How do I write a macro to Conditional Format a cell rang

Post by geecee »

Hans and Rudi.

Thanks for your replies.
Rudi wrote:Why do you want a macro?
Conditional formatting will dynamically highlight duplicates if it is set on the range you refer to?

Please explain the reason for a macro request?
If I use Conditional Formatting by clicking Conditional Formatting>Highlight Cell Rules>Duplicate Values>Red Border yes that's OK but can I save it for use the next time I want to use it.?

The reason for a macro request was for automation.
George

When we're gaun up the hill o’ fortune, may we ne'er meet a frien' comin' doun!
(When we are going up the hill of fortune, may we never meet a friend coming down!)

:smile: Don't cry because it's over...Smile because it happened.l :smile:

:note: At the end of the day it's midnight. :note:


User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How do I write a macro to Conditional Format a cell rang

Post by HansV »

Conditional formatting is saved with the workbook...
Best wishes,
Hans

User avatar
geecee
3StarLounger
Posts: 357
Joined: 02 Jun 2013, 05:55
Location: Australia - SOUTH MORANG - A northern suburb of the city of MELBOURNE in the state of Victoria

Re: How do I write a macro to Conditional Format a cell rang

Post by geecee »

HansV wrote:Conditional formatting is saved with the workbook...
Thanks Hans.
George

When we're gaun up the hill o’ fortune, may we ne'er meet a frien' comin' doun!
(When we are going up the hill of fortune, may we never meet a friend coming down!)

:smile: Don't cry because it's over...Smile because it happened.l :smile:

:note: At the end of the day it's midnight. :note: