Conditional Formatting VBA Help

davidhall

Board Regular
Joined
Mar 6, 2011
Messages
174
I need help with some conditional formatting with VBA.

I've been reviewing several post similar to what I need but don't know how to put the code together.


This is what I need.

I need the cells from rows B1 to B184 to have a clear fill color if they are blank.

Then I need any duplicate values in the same range of cells B1 to B184 to change to a green fill color.

Then I need any unique values in the same range of cells B1 to B184 to change to a light blue fill color.

Please help
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You don't need VBA for that since you really only have two colors
create the conditional format with formula is in the condition1 drop down
=COUNTIF($B$1:$B$184,B1)>2
add another condition
=COUNTIF($B$1:$B$184,B1)>=1
 
Last edited:
Upvote 0
Hi,

This i get with the macro recorder

Try it in a test-workbook

Code:
Sub Macro1()
'
' Macro1 Macro
'
'
    Range("B1:B184").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1="""""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -4.99893185216834E-02
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($B$1:$B$184;B1)>1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($B$1:$B$184;B1)=1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.799981688894314
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

HTH

M.
 
Upvote 0
I need it to be automated when certain data is copied and pasted in the worksheet so yeah VB would be best since making conditional formatting changes when recording a macro doesn't seem to work.
 
Upvote 0
I need it to be automated when certain data is copied and pasted in the worksheet so yeah VB would be best since making conditional formatting changes when recording a macro doesn't seem to work.

could you explain that a little more???
 
Upvote 0
I need it to be automated when certain data is copied and pasted in the worksheet so yeah VB would be best since making conditional formatting changes when recording a macro doesn't seem to work.

You can use the macro i've showed, but also is possible to use formulas and copy + paste only values doesnt erase your CF.

M.
 
Upvote 0
Sorry, my reply posted before I was able to see your comment.

Thanks for the VBA. I need to insert this in an existing macro. So I copy and paste it in the VBA for the macro.

If this code needs to reference data on a certain worksheet, where do I put that refence in the VBA you wrote?
 
Upvote 0
Also,

I have date on another worksheet that references data through a vlookup function. Whenever I copy and paste data in the original worksheet, the vlookup function range that was initially designated changes to #REF. Why is that?
 
Upvote 0
Sorry, my reply posted before I was able to see your comment.

Thanks for the VBA. I need to insert this in an existing macro. So I copy and paste it in the VBA for the macro.

If this code needs to reference data on a certain worksheet, where do I put that refence in the VBA you wrote?

If you want to refer to Sheet2, for example, insert this line as the first one of the macro

Sheets("Sheet2").Activate

M.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

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