VB conditional formatting problem

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I have written this code below but no matter whether C4 = V3 or not it always formats it to red (colorindex 3) Can anyone see a glowing mistake of mine please

Private Sub Worksheet_Change(ByVal Target As Range)
Set DayOne = Range("C6:D50")
For Each Cell In DayOne

If Range("C4") <> Range("V3") Then
Cell.Interior.ColorIndex = 1

End If

If Range("C4") = Range("V3") Then
Cell.Interior.ColorIndex = 3

End If

Next

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If C4 and V3 are equal, then you will ALWAYS get red color. Change logic.
 
Last edited:
Upvote 0
Maybe

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
For i = 6 To 50
    If Range("D" & i).Value <> Range("V" & i - 1).Value Then
        Range("C" & i).Resize(, 2).Interior.ColorIndex = 1
    Else
        Range("C" & i).Resize(, 2).Interior.ColorIndex = 3
    End If
Next i
End Sub
 
Upvote 0
That's right though. If C4 = V3 then I want it to be red, otherwise I want it to be another colour
 
Upvote 0
I have written this code below but no matter whether C4 = V3 or not it always formats it to red (colorindex 3) Can anyone see a glowing mistake of mine please

Your code works fine for me. What kind of values are in C4 and V3 and how do those values get into the cells (formula, typed, copied from somewhere, etc.)? By the way, your code can be simplified quite a bit...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Range("C6:D50").Interior.ColorIndex = 1 - 2 * (Range("C4").Value = Range("V3").Value)
End Sub

However, this is not the code I would use as it runs every time a change is made on the worksheet. You should make the code run only when a change is made in C4 or V3; however, to write the code to do that, we need to know whether C4 and V3 have constant values or values resulting from formulas in them.
 
Last edited:
Upvote 0
Oddly enough VoG that has a rather strange effect, some cells colour red and some colour black whether the 2 ranges equal each other or not....its a mixture now
 
Upvote 0
Hello Rick

C3 is a date, formatted as a number, it is coming from a formula simply saying "=C4"


V3 is a date as well formatted as a number, it is coming from a formula saying

=INDEX(C5:P5,MATCH(T3,T3,0))
 
Upvote 0
C3 is a date, formatted as a number, it is coming from a formula simply saying "=C4"
Why are you telling us about C3 (your test is for C4 against V3)?

V3 is a date as well formatted as a number, it is coming from a formula saying

=INDEX(C5:P5,MATCH(T3,T3,0))

Two things to check:

1) Are both cells that you are checking returning pure dates... don't look at the displayed (formatted) values, look at the underlying values... are there time values in either one?

2) Are both cells values actually numbers or are they perhaps actually text values (the number format won't change a text value into a numeric value)?

In the code you posted originally, change your test to this and see if it makes a difference...
Code:
If CLng(Range("C4")) <> CLng(Range("V3")) Then
 
Upvote 0
Hello Rick, yes, both dates are pure dates, there is no time element in the result and both are also purely numeric...I attach my spreadsheet example

Tailormade Day Plan 2.xls
BCDEFGHIJKLMNOPQRSTUV
2
3EnterDate:14/06/2011ChooseDayofWeek:Wednesday40708
440708407094071040711407124071340714
5UploadfromRosterTuesdayWednesdayThursdayFridaySaturdaySundayMondayNameDuty1Timeto
6            AkshadaChandorkarAlpha07:0013:00
7KarenKnapton              AlainFrimigacci
8MikeGwynne              AlesZelenka
Data



To explain what I need it to do is, to highlight anything in grey which is not the date selected in T3

So in the drop down if you select Wednesday, all the other days of the week (columns C:O) appear in grey (was using red as an example thats all)


The date in C4 is exactly what is in C5 (which comes from C3) , the reason for having the date in C5 as well as C4 is only because C5 and D5 are merged and I initially thought this may have caused the problem

The drop down in T3 is text only, so I did a look up to return a date in V3 as you can see


I changed my code as you suggested but still made no difference

Private Sub Worksheet_Change(ByVal Target As Range)
Set DayOne = Range("C6:D50")
For Each Cell In DayOne

If CLng(Range("C4"))<> CLng(Range("V3")) Then
Cell.Interior.ColorIndex = 1

End If

If CLng(Range("C4")) = CLng(Range("V3")) Then
Cell.Interior.ColorIndex = 3

End If

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,328
Members
452,907
Latest member
Roland Deschain

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