×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

FormatConditions for ColorIndexes in Excel

FormatConditions for ColorIndexes in Excel

FormatConditions for ColorIndexes in Excel

(OP)
Hi,

I have the following code to chagne the the colors of alternating rows. Even rows are colored Green(Index of 4) and odd rows are colored yellow (index of 6). Is there any way to modify these conditions to color Row 1 a different color, say blue (index of 33)?

CODE

Worksheets("Sheet1").Cells.Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
Selection.FormatConditions(1).Interior.ColorIndex = 4
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
Selection.FormatConditions(2).Interior.ColorIndex = 6

RE: FormatConditions for ColorIndexes in Excel

This is not tested, but based on your code, since you can have up to three conditions, you should be able to modify it like this:

CODE

Worksheets("Sheet1").Cells.Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=ROW()=1"
Selection.FormatConditions(1).Interior.ColorIndex = 33
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
Selection.FormatConditions(1).Interior.ColorIndex = 4
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
Selection.FormatConditions(2).Interior.ColorIndex = 6

RE: FormatConditions for ColorIndexes in Excel

(OP)
I tried adding a third condition and teh thrid condition overruled the first. Since row 1 is an odd row, then the last condition take precedent.
But i found a better way of doing it.
With later  defining the color for Row 1 when i define the content of the row.
Thanks

CODE

If x Mod 2 = 0 Then
       Worksheets("Sheet1").Cells(x,1).EntireRow.Interior.ColorIndex = 4
    ElseIf x Mod 2 = 1 Then
        Worksheets("Sheet1").Cells(x,1).EntireRow.Interior.ColorIndex = 6
    End If

RE: FormatConditions for ColorIndexes in Excel

Sorry you couldn't get it to work.  As I said it was untested.  All that was needed to fix it was to update the index numbers like this (slightly rearranged for clarity):

CODE

  With Worksheets("Sheet1").Cells
    With .FormatConditions
      .Delete
      .Add Type:=xlExpression, Formula1:="=ROW()=1"
      .Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
      .Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
    End With
    .FormatConditions(1).Interior.ColorIndex = 33
    .FormatConditions(2).Interior.ColorIndex = 4
    .FormatConditions(3).Interior.ColorIndex = 6
  End With

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close