conditional formatting by code?

fry

Active Member
Joined
Apr 25, 2007
Messages
411
Hi All

I've just played around with conditional formatting trying to make a row of cells (B4 to K4) turn red when one of the cells (J4) has an entry but I'm failing fast.

Can this be done automatically by code??

Thanks :)
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
You can do it with this code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("J4").Value <> "" Then
    Range("B4:K4").Interior.ColorIndex = 3
    Else
        Range("B4:K4").Interior.ColorIndex = xlColorIndexNone
End If
End Sub

But for conditional formatting:
Highlight the range B4:K4, select Conditional Formatting:
Formula Is:
Code:
=$J$4<>""
 

fry

Active Member
Joined
Apr 25, 2007
Messages
411
Hi Lewiy

Oh dear, slight problem :( Although the above does work (thank you for that) it doesn't do just that row, it does everything.........my fault, I didn't think this one through.

I've just realised that its only J4 on the first line (DOH!!).......then J5 for the next line, J6 for the next etc............I think my mind has gone at last!?!?

Oops springs to mind :(
 

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Ok, if I understand correctly then try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 10 And Target.Row >= 4 Then
    If Target.Value <> "" Then
        Range("B" & Target.Row & ":K" & Target.Row).Interior.ColorIndex = 3
        Else
            Range("B" & Target.Row & ":K" & Target.Row).Interior.ColorIndex = xlColorIndexNone
    End If
End If
End Sub

I have assumend that you don't want it to act on rows 1 to 3.
 

fry

Active Member
Joined
Apr 25, 2007
Messages
411
Hi Lewiy

I think I've sorted it. My brain got into gear for 5 mins.

What I did was to change your =$J$4<>"" to =$J4<>"" and enter this in conditional formatting on the first row (B4:K4).

Then I highlighted the same row and copied, after which I highlighted the row below (B5:K5) and dragged down to B500:K500. I then did a paste special and pasted the format only..........Hey Presto it seemed to work!!

I wouldn't have been able to do that without your suggestion though so I thank you very much for your time and patience with me. :)
 

Forum statistics

Threads
1,181,053
Messages
5,927,848
Members
436,573
Latest member
CMR237

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
Top