+ Reply to Thread
Results 1 to 5 of 5

conditional formatting w/ check for beginning odd or even

Hybrid View

  1. #1
    barberboy@gmail.com
    Guest

    conditional formatting w/ check for beginning odd or even

    Greeting Excel programmers. I have a simple question.

    I have two macros that i use to add a conditional format to highlight
    every other row for the cells that are selected. One macro gives the
    EVEN rows (e.g. 2,4,6) a blue background and ODD (e.g. 1,3,5) rows a
    white background. The other does the opposite: ODD rows blue, EVEN rows
    odd. (The only thing different about them is the '=' vs the '>' in the
    conditional formatting function.)

    What i would like is a macro that will check to see if the FIRST row in
    the selection is odd, and if it is, apply the EvenRowsBlue macro. If it
    is even, apply the OddRowsBlue macro.

    These are the two macros i have now, but if there is a different/better
    way without them, i am up for that.

    Sub OddRowsBlue()
    '
    ' Created by barberboy, 18-12-05

    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)>0"
    .FormatConditions(1).Interior.ColorIndex = 34
    .Interior.ColorIndex = 2
    End With
    End Sub

    Sub EvenRowsBlue()
    '
    ' Created by barberboy, 18-12-05

    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)=0"
    .FormatConditions(1).Interior.ColorIndex = 34
    .Interior.ColorIndex = 2
    End With
    End Sub

    Thanks in advance!!


  2. #2
    Registered User
    Join Date
    12-20-2005
    Posts
    5
    Quote Originally Posted by barberboy@gmail.com
    Sub OddRowsBlue()
    '
    ' Created by barberboy, 18-12-05

    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)>0"
    .FormatConditions(1).Interior.ColorIndex = 34
    .Interior.ColorIndex = 2
    End With
    End Sub

    Sub EvenRowsBlue()
    '
    ' Created by barberboy, 18-12-05

    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)=0"
    .FormatConditions(1).Interior.ColorIndex = 34
    .Interior.ColorIndex = 2
    End With
    End Sub
    Sub RowsBlue()
        With Selection
            .FormatConditions.Delete
            If Selection.Cells(1,1).Row Mod 2 > 0 Then 
                .FormatConditions.Add Type:=xlExpression, Formula1:= _
                    "=MOD(ROW(),2)>0"
            Else
                .FormatConditions.Add Type:=xlExpression, Formula1:= _
                    "=MOD(ROW(),2)=0"
            End If
            .FormatConditions(1).Interior.ColorIndex = 34
            .Interior.ColorIndex = 2
        End With
    End Sub
    This should effectively always colour your first row blue, regardless if it's even or odd.

  3. #3
    Tom Ogilvy
    Guest

    Re: conditional formatting w/ check for beginning odd or even

    Sub WhatYouAskedfor()
    If Selection(1).Row Mod 2 = 1 Then
    'first row is odd
    EvenRowBlue
    Else
    ' first row is even
    OddRowsBlue
    End If
    End Sub

    A possible alternative:

    Sub abcEven()
    Dim even As Long, odd As Long
    If Selection(1).Row Mod 2 = 1 Then
    odd = 34
    even = 2
    Else
    odd = 2
    even = 34
    End If
    With Selection
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)>0"
    .FormatConditions(1).Interior.ColorIndex = even
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)=0"
    .FormatConditions(2).Interior.ColorIndex = odd
    End With
    End Sub

    --
    Regards,
    Tom Ogilvy

    <barberboy@gmail.com> wrote in message
    news:1135098054.346818.262210@g43g2000cwa.googlegroups.com...
    > Greeting Excel programmers. I have a simple question.
    >
    > I have two macros that i use to add a conditional format to highlight
    > every other row for the cells that are selected. One macro gives the
    > EVEN rows (e.g. 2,4,6) a blue background and ODD (e.g. 1,3,5) rows a
    > white background. The other does the opposite: ODD rows blue, EVEN rows
    > odd. (The only thing different about them is the '=' vs the '>' in the
    > conditional formatting function.)
    >
    > What i would like is a macro that will check to see if the FIRST row in
    > the selection is odd, and if it is, apply the EvenRowsBlue macro. If it
    > is even, apply the OddRowsBlue macro.
    >
    > These are the two macros i have now, but if there is a different/better
    > way without them, i am up for that.
    >
    > Sub OddRowsBlue()
    > '
    > ' Created by barberboy, 18-12-05
    >
    > With Selection
    > .FormatConditions.Delete
    > .FormatConditions.Add Type:=xlExpression, Formula1:= _
    > "=MOD(ROW(),2)>0"
    > .FormatConditions(1).Interior.ColorIndex = 34
    > .Interior.ColorIndex = 2
    > End With
    > End Sub
    >
    > Sub EvenRowsBlue()
    > '
    > ' Created by barberboy, 18-12-05
    >
    > With Selection
    > .FormatConditions.Delete
    > .FormatConditions.Add Type:=xlExpression, Formula1:= _
    > "=MOD(ROW(),2)=0"
    > .FormatConditions(1).Interior.ColorIndex = 34
    > .Interior.ColorIndex = 2
    > End With
    > End Sub
    >
    > Thanks in advance!!
    >




  4. #4
    barberboy@gmail.com
    Guest

    Re: conditional formatting w/ check for beginning odd or even

    Thanks Tom and Excelibur. Both abcEven and RowsBlue do EXACTLY what i
    am needing. You guys are great!!


  5. #5
    barberboy@gmail.com
    Guest

    Re: conditional formatting w/ check for beginning odd or even

    Final Macro:

    Sub BlueRows()
    With Selection
    .FormatConditions.Delete
    If Selection.Cells(1, 1).Row Mod 2 = 0 Then
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)>0"
    Else
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(ROW(),2)=0"
    End If
    .FormatConditions(1).Interior.ColorIndex = 34
    .Interior.ColorIndex = 2
    End With
    End Sub


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1