VBA Question on highlighting Values

philwojo

Well-known Member
Joined
May 10, 2013
Messages
533
I am trying to get my VBA code to highlight a cell Red if it doesn't have a certain set of values. But currently it is making everything Red, not what I am expecting. Can someone suggest how I can correct this.

Code:
Sub RecordValueCheck()

Dim icount As Integer, Cell As Range


Application.EnableEvents = False
Application.ScreenUpdating = False


Worksheets("PIF Checker Output - Horz").Activate


icount = 0
' payvalue = 2100


    
With ThisWorkbook.Worksheets("PIF Checker Output - Horz")
    For Each Cell In .Range("B23", .Range("B" & Rows.Count).End(xlUp))
        If Cell.Value <> 1000 Or Cell.Value <> "1000" Or Cell.Value <> 2100 Or Cell.Value <> 3000 Or Cell.Value <> 5000 Then
            Cell.Interior.Color = vbRed
            Cell.Font.Bold = True
            Cell.Font.Color = vbYellow
        End If
    Next
End With


Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub

I am also open to making the code better, but this is at least kind of working.

Thanks,
Phil
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You need to swap Or for And
 
Upvote 0
OK that did the trick, but just so I am thinking clearly, if I was doing this via a formula, not using VBA, I would use OR there, correct?

Meaning the formula would be something like
Code:
=If(b23<>or(1000,2100,3000,5000),"Good","Bad")

I think that is why I went with OR, just want to make sure my logic is right here.

Thanks for the help though.
 
Upvote 0

Forum statistics

Threads
1,215,503
Messages
6,125,179
Members
449,212
Latest member
kenmaldonado

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