+ Reply to Thread
Results 1 to 8 of 8

Thread: Macro for Conditional Formatting

  1. #1
    Registered User
    Join Date
    12-29-2009
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    16

    Macro for Conditional Formatting

    Hello. I need to make a macro that will apply several conditional formatting items to whatever area I have selected. The formulas are as follows assuming that I selected large chunks of columns AR through to AV:

    =IF(SUM($AR2:$AV2)>0,1,0)
    =IF((COUNTIF($AR2:$AV2,"C")>0),1,0)
    =IF(SUM($AR2:$AV2)<1,1,0)

    Each one needs to set the cells to a custom colour... I assume that I can tweak this part of the macro myself once I see the logic.

    Thanks - I'm really stuck.

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: Macro for Conditional Formatting

    Hi Humanist, have you tried just recording a macro while you select a range and apply those conditonal formatting formulas? It will show you the exact code layout you need to apply it to other ranges. (Note: CF coding and architecture is different between Excel 2003 and 2007/2010.)

  3. #3
    Registered User
    Join Date
    12-29-2009
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Macro for Conditional Formatting

    I did try recording, but the macro seems to record and define the actual cells I selected. I guess I was hoping that the macro would allow me to work with any range. Does that make sense?

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    MS 2007
    Posts
    5,372

    Re: Macro for Conditional Formatting

    If there are only 3 conditions, why not use the native Conditional Formatting.
    Note: With CF, you do not need the IF statements, i.e. select your ranges starting with row 2 and
    Condition 1 = SUM($AR2:$AV2)>0
    Condition 2 = COUNTIF($AR2:$AV2,"C")>0)
    Condition 3 = SUM($AR2:$AV2)<1

    Does that work for you?
    ChemistB
    My 2¢

    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

  5. #5
    Registered User
    Join Date
    12-29-2009
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Macro for Conditional Formatting

    Quote Originally Posted by ChemistB View Post
    If there are only 3 conditions, why not use the native Conditional Formatting.
    Note: With CF, you do not need the IF statements, i.e. select your ranges starting with row 2 and
    Condition 1 = SUM($AR2:$AV2)>0
    Condition 2 = COUNTIF($AR2:$AV2,"C")>0)
    Condition 3 = SUM($AR2:$AV2)<1

    Does that work for you?
    The problem is that these formulas evaluate one school week for each student, turning data red if no result is achieved (3) and yellow if no result is achieved but I remembered to call home (2). So every week I need to define 5 new columns and re-apply the same fomulas, but in a different set of conditonals. (There may soon be a 4th)

    If I understand correctly, your suggestion would assess all colums at once, but I need to assess 40 sets of 5 seperately...and to be gradulally adding sets (up to 40) week-by-week.

    I hope I am being clear. If not, I can upload what I'm doing....

  6. #6
    Registered User
    Join Date
    12-29-2009
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Macro for Conditional Formatting

    Really, I guess I just need to know if there is something I can substitute into this line:

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=SUM($J2:$N2)<1"

    To make $J and $N dynamic based on my selection. The "2" will always be true.

    and to change
    Range("E2:I5").Select
    so that the entire range is taken from what was already selected and acted upon in the previous conditonal.
    Last edited by Humanist; 10-26-2010 at 08:17 PM.

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    MS 2007
    Posts
    5,372

    Re: Macro for Conditional Formatting

    I'm not getting a visual. Best to upload an example.
    ChemistB
    My 2¢

    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

  8. #8
    Registered User
    Join Date
    12-29-2009
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Macro for Conditional Formatting

    Attached please find a small part of the spreadsheet in question. The real one has much more data on the right and a LOT more students.

    As you may see, each school day is grouped into weeks. Into each day I write the number of hours the student was in class or a "C" if I called the student.

    Each week has 3 conditions:
    • No format if a number is present.
    • Yellow if no numbers are present but a C is present (They didn't show, but at least I took action)
    • Red if there is no number and no C in that week.

    The idea is that red bars (each being a week of no attendance for a single student) will stand out as problems to be solved. Yellow bars will alert me to a problem (no attendance for that week) but where action is being taken in the form of a phone call home. Each week needs to be analyzed seperately.... although I am considering adding another layer where 3 weeks in a row of non-attendance results in a brighter red.

    Everytime I define 5 new columns as the new school week, it is a big pain to add the 3 conditons over and over again. That is why I'd like a macro or some other solution.

    I hope this is clear now. I teach in a weird school, so the set-up is not something you'd expect from normal classes.
    Attached Files Attached Files
    Last edited by Humanist; 10-27-2010 at 02:09 PM.

+ 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.2.0