Row Color change with VB Code based on a cell in each row

awagdarikar

Board Regular
Joined
Jun 20, 2008
Messages
115
Dear Excel / VB Experts,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I want to change color of each row in data range A1:AF500 based on the number in respective row in the AG Column. For example, the color of row A1:AF1 should change with the number entry in cell AG1 and so on for all the rows till row 500. I know this can be easily achieved with Conditional Formatting but the hitch is my conditions are more than 3. The conditions are as follows,<o:p></o:p>
<o:p></o:p>
  • If Cell Value is 5 then the respective row color has to be changed to Turquoise (Color Index 8) <o:p></o:p>
  • If Cell Value is 6 or 7 or 8, then the respective row color has to be changed to Lavender (Color Index 39) <o:p></o:p>
  • If Cell Value is 9 then the respective row color has to be changed to Yellow (Color Index 6)<o:p></o:p>
  • If Cell Value is 10 then the respective row color has to be changed to Green (Color Index 4)<o:p></o:p>
  • No action to be taken if the cell contains any other value than the conditions listed above<o:p></o:p>
I do not know VB much so I searched through the Mrexcel Archives and Google but could not get one code which could satisfy my needs. There are some codes available on net and in Mrexcel but not being a VB Expert, I am not able to edit them.

Can somebody help me with a simple VB program which I can use to achieve this?<o:p></o:p>
<o:p></o:p>
Thanks a ton in advance,<o:p></o:p>
<o:p></o:p>
awagdarikar<o:p></o:p>
<!-- / message -->
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Range("AG" & Target.Row).Value
    Case 5
        Range("A" & Target.Row & ":AF" & Target.Row).Interior.ColorIndex = 8
    Case 6, 7, 8
        Range("A" & Target.Row & ":AF" & Target.Row).Interior.ColorIndex = 39
    Case 9
        Range("A" & Target.Row & ":AF" & Target.Row).Interior.ColorIndex = 6
    Case 10
        Range("A" & Target.Row & ":AF" & Target.Row).Interior.ColorIndex = 4
    Case Else
        Range("A" & Target.Row & ":AF" & Target.Row).Interior.ColorIndex = xlNone
End Select
End Sub
 
Upvote 0
If you wanted to do this in one go and not via an event procedure try

Code:
Sub ColRow()
Dim I As Long, Icol As Integer
For I = 1 To 500
    Select Case Range("AG" & I).Value
        Case 5
            Icol = 8
        Case 6, 7, 8
            Icol = 39
        Case 9
            Icol = 6
        Case 10
            Icol = 4
        Case Else
            Icol = xlNone
    End Select
    Range("A" & I & ":AF" & I).Interior.ColorIndex = Icol
Next I
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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